Package: colorutils
A library which provides utilities for working with colors in Python. Colors are modeled by theColorclass and can be
represented inRGB,HEX,WEB, andYIQformats.0. Installationcolorutilscan be installed from pypi:pip install colorutilsto update an existing installation from pypi to the latest version:pip install colorutils --upgrade1. Current Featuresv0.2.1(released 05/25/2015)Bug fix for pip install on Windows for unfound packagesv0.2(released 05/23/2015)Add HSV color representation and conversionsAdd YIQ color representation and conversionsColor objects can be treated as iterablesImplementation of color runsAddition of pre-defined color palettesPackage restructuringv0.1(released 05/16/2015)A versatile abstract color model which allows color addition and subtractionConversions between:RGBtuples, 6-characterHEXstrings, 3-characterHEXstrings, andWEBrepresentations of color.Random color generation2. Reporting Bugs / Requesting FeaturesTo report a bug or request a feature for colorutils, please open a newissue3. Usage3.1 Instantiating aColorThe basic way to instantiate aColor:>>> from colorutils import Color
>>> c = Color((255, 255, 255))
>>> c
<Color (255, 255, 255)>By default, the Color object expects anRGB3-tuple, but there are multiple ways to instantiate aColor. The possibilities for Cyan, for example:Color((0, 255, 255))
Color(green=255, blue=255)
Color(rgb=(0, 255, 255))
Color(hex='#00FFFF')
Color(hex='00ffff')
Color(hex='#0ff')
Color(hex='0ff')
Color(web='cyan')
Color(web='Cyan')
Color(web='CYAN')
Color(web='#00ffff')
Color(web='#0ff')
Color(yiq=(0.701, -0.596, -0.217))
Color(hsv=(180, 1, 1))Colorobjects can also take the color from otherColorobjects:>>> Color(Color((255, 255, 255)))
<Color (255, 255, 255)>

>>> Color(Color(Color(Color((255, 255, 255)))))
<Color (255, 255, 255)>3.2 Color ConversionThe current color models supported bycolorutilsare:RGB,HEX,WEB,YIQ, andHSV. Each instantiatedColorobject has properties which will automatically perform the required conversions:>>> c = Color((46, 139, 87))

>>> c.red
46

>>> c.green
139

>>> c.blue
87

>>> c.rgb
(46, 139, 87)

>>> c.hex
'#2e8b57'

>>> c.shorthex
'#2e8b57'

>>> c.web
'SeaGreen'

>>> c.yiq
(0.413, -0.152, -0.143)

>>> c.hsv
(146.452, 0.669, 0.545)If the color were such that theHEXrepresentation could be captured as a 3-char hex:>>> c = Color((0, 0, 0))

>>> c.hex
'#000000'

>>> c.shorthex
'#000'The web representation will return the hex value if the color is not a well-known named web color:>>> c = Color((1, 243, 77))

>>> c.hex
'#01f34d'

>>> c.web
'#01f34d'These same conversions can be done without instantiating aColorobject by using the static methods:rgb_to_hex()rgb_to_web()rgb_to_yiq()rgb_to_hsv()hex_to_rgb()hex_to_web()hex_to_yiq()hex_to_hsv()web_to_rbg()web_to_hex()web_to_yiq()web_to_hsv()yiq_to_rgb()yiq_to_hex()yiq_to_web()yiq_to_hsv()hsv_to_rgb()hsv_to_hex()hsv_to_web()hsv_to_yiq()Using these static conversion methods, one can chain conversions (due to the in-param and out-param of all multi-value color representations being a tuple), which you are unable to do using the Python defaultcolorsys.:>>> rgb_to_hex(hex_to_rgb('#808080'))
'#808080'Of course, be wary of chaining. Since approximation exists in the conversion algorithms, degradation will occur:>>> yiq_to_web(rgb_to_yiq(hex_to_rgb('808080')))
'#7f807e'Though, the values will still be close:>>> hex(int('80', 16) - int('7f', 16))  # Red difference
'0x1'

>>> hex(int('80', 16) - int('80', 16))  # Green difference
'0x0'

>>> hex(int('80', 16) - int('7e', 16))  # Blue difference
'0x2'3.3ColorArithmeticAlthough the addition and subtraction of color does not always make sense, the ability to do so is supported. There are two additive models currently supported:LIGHTandBLEND.3.3.1 AdditionLIGHTthe light model is an additive model, where the rgb components are added, but do not exceed the maximum value, 255. This model is the default model which everyColoris initialized with, unless overridden.An example ofLIGHTaddition:>>> Color((0, 100, 200)) + Color((100, 100, 100))
<Color (100, 200, 255)>BLENDthe blend model is an averaging model, where each rgb component is averaged.An example ofBLENDaddition:>>> Color((0, 100, 200), arithmetic=ArithmeticModel.BLEND) + Color((100, 100, 100))
<Color (50, 150, 250)>When assigning models, it is important to note that the arithmetic model for the first object in the operation, e.g. Object1 in ‘Object1 + Object2’, is the model which will be used when computing the addition.Coloraddition can also operate on 3-tuples, which represent anRGBvalue:>>> Color((50, 50, 50)) + (20, 20, 20)
<Color (70, 70, 70)>3.3.2 SubtractionThere is currently only one subtractive model, the equivalent to the inverse of theLIGHTadditive model. There is no model representing the inverse ofBLEND, since the inverse average does not really make sense.:>>> Color((100, 100, 100)) - Color((0, 75, 200))
<Color (100, 25, 0)>Colorsubtraction can also operate on 3-tuples, which represent anRGBvalue:>>> Color((50, 50, 50)) - (20, 20, 20)
<Color (30, 30, 30)>3.4 Color EqualityTesting for equality between colors defaults to testing between the equality of theRGBvalues:>>> c1 = Color((10, 20, 30))
>>> c2 = Color((10, 20, 30))
>>> c3 = Color((10, 20, 20))

>>> c1 == c2
True

>>> c1 == c3
FalseDifferent equality functions can be set, using either the predefined equalities incolorutils.equality, or from a custom equality function:>>> from colorutils.equality import *
>>> c = Color((10, 20, 30), equality_fn=RED_eq)
>>> c2 = Color((10, 40, 60))

>>> c == c2
True

>>> c2 == c
FalseNotice that in the above example, when checking for red equality, when the color with theRED_eqequality comes first in the comparison, it
evaluates toTrue. If it comes second, it evaluates toFalse.  This is because the equality function of the firstColorinstance in
the comparison defines which equality function is used.The predefined equalities are:RGB_eqRED_eqGREEN_eqBLUE_eqHEX_eqWEB_eqYIQ_eqHSV_eqDefining a custom equality would follow the pattern defined by the RGB_eq definition, below:RGB_eq = lambda c1, c2: c1.rgb == c2.rgb3.5 Color PalettesA collection of pre-defined color palettes exists for convenience. The palettes which are currently implemented include:grayscaleprimaryrgbroygbvsecondaryIndividual named colors can be used from the palettes, or all colors can be retrieved:>>> import colorutils.palettes.primary as primary

>>> primary.red
<Color (255, 0, 0)>

>>> primary.yellow
<Color (255, 255, 0)>

>>> primary.blue
<Color (0, 0, 255)>

>>> primary.all
[<Color (255, 0, 0)>, <Color (255, 255, 0)>, <Color (0, 0, 255)>]4.colorutilsvs othersTo see how thecolorutilsconversion algorithms compare to other algorithms/provided values, see thecomparisonswiki page.