edaniszewski / colorutils Goto Github PK
View Code? Open in Web Editor NEWA library which provides utilities for working with colors in Python
License: MIT License
A library which provides utilities for working with colors in Python
License: MIT License
Hi,
provided the code below, I get a rgb tuple with some values higher than 255.
Is this expected?
The version of colorutils I am using is 0.2.1.
Thanks,
Jose Manuel
import colorutils
from colorutils import Color
from colorutils import ArithmeticModel
c1 = Color(rgb=[255, 153, 153]) # red
c2 = Color(rgb=[102, 153, 255]) # blue
c3 = c1 + c2
c4 = Color(rgb=c1.rgb, arithmetic=ArithmeticModel.BLEND) + c2
c5 = Color(rgb=c2.rgb, arithmetic=ArithmeticModel.BLEND) + c1
for i, c in enumerate([c1, c2, c3, c4, c5):
display(f"c{str(i+1)}: {c.rgb} : {c.hex}")
c1: [255, 153, 153] : #ff9999'
c2: [102, 153, 255] : #6699ff'
c3: (255, 255, 255) : #ffffff'
c4: (306, 229, 280) : #132e5118'
c5: (229, 229, 331) : #e5e514b'
Have predefined color palattes available to users. e.g. perhaps something like:
> from colorutils.palattes import grayscale
> grayscale.__dict__
[Color(0,0,0), Color(10,10,10), Color(20,20,20), ......]
> from colorutils.palattes import primary
> primary.__dict__
[Color(255,0,0), Color(255,255,0), Color(0,0,255), ......]
potential palattes:
cymk
rgb
primary
secondary
roygbv
grayscale
I actually just made a fork with a very basic implementation of this for my own use, although I don't think the code I have is exactly ready for prime time or anything.
But basically, by adding a function like _repr_html_
to the Color
object, interactive notebook environments like Jupyter or IPython can provide a richer display representation for these objects; in this case it would be quite useful to use this to provide a color swatch of some sort.
The code I added currently just displays a colored box with the hex code above it, like this:
I'm sure someone else could figure out an even neater layout for this (perhaps put the hex code, rgb triple, hsv, etc to the right of the swatch?), though.
For reference, to get this I added this function to the definition of Color
:
def _repr_html_(self):
return '''
<div style="
width:80px;
text-align:center;
">
<code>{0}</code>
</div>
<div style="
background-color:{0};
width:80px;
height:80px;
border-style:solid;
border-width:2px
"></div>'''.format(self.hex)
create algorithm that decides on best text color (takes two color, either default black, white, or can be some dark-light overridden color) based on some background color. e.g. if given a dark background, return the light text color. light background would yield dark text color
given a color, make a color triad with that color, see triad section: http://www.tigercolor.com/color-lab/color-theory/color-harmonies.htm
192.168.0.165> ./ColorMix red blue
Traceback (most recent call last):
File "./ColorMix", line 11, in
Col1 = Color(web=sys.argv[1], arithmetic = ArithmeticModel.BLEND)
NameError: name 'ArithmeticModel' is not defined
192.168.0.165> cat ColorMix
#!/usr/bin/python3
import sys
from colorutils import Color
print ("\n")
Col1 = Color(web=sys.argv[1], arithmetic = ArithmeticModel.BLEND)
Col2 = Color(web=sys.argv[2], arithmetic = ArithmeticModel.BLEND)
Col3 = Col1 + Col2
print(Col1.web, " + ", Col2.web, " = ", Col3.web)
print ("\n")
quit()
This may be an internal code syntax error, but how can I sum the list of Color
objects?
Separate index summation works, but function doesn't.
>>> from colorutils import Color, ArithmeticModel
>>>
>>> # An example of LIGHT addition:
>>> # >>> Color((0, 100, 200)) + Color((100, 100, 100))
>>> # <Color (100, 200, 255)>
>>>
>>> List = [(63, 49, 39), (73, 97, 138), (56, 68, 91)]
>>> List = [Color(x) for x in List]
>>> List = sum(List)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'Color'
>>>
>>> List[0] + List[1] + List[2]
<Color (192, 214, 255)>
supposedly HSV is a better color model than RGB.. maybe worth implementing and changing the underlying Color class model to HSV. Adding support includes adding conversions between all existing color formats
the only valid types for addition and subtraction are Color. Should be able to add/subtract rgb tuples, e.g. Color(0,0,0) + (1, 1, 1) = Color(1, 1, 1)
it should be great that rgb model be improved and support for the alpha channel
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.