Package: colouralpha
Converts and manipulates common color representation (RGB, HSL, web, …)This project is forked fromColour.FeaturesDamn simple and pythonic way to manipulate color representation (see
examples below)Full conversion between RGB, HSL, 6-digit hex, 3-digit hex, human colorOne object (Color) or bunch of single purpose function (rgb2hex,hsl2rgb…)webformat that use the smallest representation between
6-digit (e.g.#fa3b2c), 3-digit (e.g.#fbb), fully spelled
color (e.g.white), followingW3C color namingfor compatible
CSS or HTML color specifications.smooth intuitive color scale generation choosing N color gradients.can pick colors for you to identify objects of your application.InstallationYou don’t need to download the GIT version of the code ascolouralphais
available on the PyPI. So you should be able to run:pip install colouralphaIf you have downloaded the GIT sources, then you could add thecolour.pydirectly to one of yoursite-packages(thanks to a symlink). Or install
the current version via:poetry installAnd if you don’t have the GIT sources but would like to get the latest
main or branch from gitlab, you could also:pip install git+https://gitlab.com/marcin-serwin/colouralpha.gitOr even select a specific revision (branch/tag/commit):pip install git+https://gitlab.com/marcin-serwin/colouralpha.git@mainUsageTo get complete demo of each function, please read the source code which is
heavily documented and provide a lot of examples in doctest format.You may also access generated documentationhere.Here is a reduced sample of a common usage scenario:InstantiationLet’s create blue color:>>>fromcolourimportColor>>>c=Color("blue")>>>c<Colorblue>Please note that all of these are equivalent examples to create the red color:Color("red")## human, web compatible representationColor(red=1)## default amount of blue and green is 0.0Color("blue",hue=0)## hue of blue is 0.66, hue of red is 0.0Color("#f00")## standard 3 hex digit web compatible representationColor("#ff0000")## standard 6 hex digit web compatible representationColor(hue=0,saturation=1,luminance=0.5)Color(hsl=(0,1,0.5))## full 3-uple HSL specificationColor(rgb=(1,0,0))## full 3-uple RGB specificationColor(Color("red"))## recursion doesn't break objectReading valuesSeveral representations are accessible:>>>c.hex'#00f'>>>c.hsl# doctest: +ELLIPSIS(0.66...,1.0,0.5)>>>c.rgb(0.0,0.0,1.0)And their different parts are also independently accessible, as the different
amount of red, blue, green, in the RGB format:>>>c.red0.0>>>c.blue1.0>>>c.green0.0Or the hue, saturation and luminance of the HSL representation:>>>c.hue# doctest: +ELLIPSIS0.66...>>>c.saturation1.0>>>c.luminance0.5A note on the.hexproperty, it’ll return the smallest valid value
when possible. If you are only interested by the long value, use.hex_l:>>>c.hex_l'#0000ff'Modifying color objectsAll of these properties are read/write, so let’s add some red to this color:>>>c.red=1>>>c<Colormagenta>We might want to de-saturate this color:>>>c.saturation=0.5>>>c<Color#bf40bf>And of course, the string conversion will give the web representation which is
human, or 3-digit, or 6-digit hex representation depending which is usable:>>>"%s"%c'#bf40bf'>>>c.luminance=1>>>"%s"%c'white'Ranges of colorsYou can get some color scale of variation between twoColorobjects quite
easily. Here, is the color scale of the rainbow between red and blue:>>>red=Color("red")>>>blue=Color("blue")>>>list(red.range_to(blue,5))[<Colorred>,<Coloryellow>,<Colorlime>,<Colorcyan>,<Colorblue>]Or the different amount of gray between black and white:>>>black=Color("black")>>>white=Color("white")>>>list(black.range_to(white,6))[<Colorblack>,<Color#333>, <Color #666>, <Color #999>, <Color #ccc>, <Color white>]If you have to create graphical representation with color scale
between red and green (‘lime’ color is full green):>>>lime=Color("lime")>>>list(red.range_to(lime,5))[<Colorred>,<Color#ff7f00>, <Color yellow>, <Color chartreuse>, <Color lime>]Notice how naturally, the yellow is displayed in human format and in
the middle of the scale. And that the quite unusual (but compatible)
‘chartreuse’ color specification has been used in place of the
hexadecimal representation.Color comparisonSane defaultColor comparison is a vast subject. However, it might seem quite straightforward for
you.Colouruses a configurable default way of comparing color that might suit
your needs:>>>Color("red")==Color("#f00")==Color("blue",hue=0)TrueThe default comparison algorithm focuses only on the “web” representation which is
equivalent to comparing the long hex representation (e.g. #FF0000) or to be more
specific, it is equivalent to compare the amount of red, green, and blue composition
of the RGB representation, each of these value being quantized to a 256 value scale.This default comparison is a practical and convenient way to measure the actual
color equivalence on your screen, or in your video card memory.But this comparison wouldn’t make the difference between a black red, and a
black blue, which both are black:>>>black_red=Color("red",luminance=0)>>>black_blue=Color("blue",luminance=0)>>>black_red==black_blueTrueCustomizationBut, this is not the sole way to compare two colors. As I’m quite lazy, I’m providing
you a way to customize it to your needs. Thus:>>>fromcolourimportRGB_equivalence,HSL_equivalence>>>Color.equality=staticmethod(HSL_equivalence)>>>black_red=Color("red",luminance=0)>>>black_blue=Color("blue",luminance=0)>>>black_red==black_blueFalseYou may also set it per instance:>>>black_red=Color("red",luminance=0,equality=HSL_equivalence)>>>black_blue=Color("blue",luminance=0,equality=HSL_equivalence)>>>black_red==black_blueFalseAs you might have already guessed, the sane default isRGB_equivalence, so:>>>Color.equality=staticmethod(RGB_equivalence)>>>black_red=Color("red",luminance=0)>>>black_blue=Color("blue",luminance=0)>>>black_red==black_blueTrueHere’s how you could implement your unique comparison function:>>>saturation_equivalence=lambdac1,c2:c1.saturation==c2.saturation>>>red=Color("red",equality=saturation_equivalence)>>>blue=Color("blue",equality=saturation_equivalence)>>>white=Color("white",equality=saturation_equivalence)>>>red==blueTrue>>>white==redFalseNote: When comparing 2 colors with equality set per instance,onlythe
equality functionof the first color will be used. Thus:>>>black_red=Color("red",luminance=0,equality=RGB_equivalence)>>>black_blue=Color("blue",luminance=0,equality=HSL_equivalence)>>>black_red==black_blueTrueBut reverse operation is not equivalent !:>>>black_blue==black_redFalseEquality to non-Colour objectsAs a side note, whatever your custom equality function is, it won’t be
used if you compare to anything else than aColourinstance:>>>red=Color("red",equality=lambdac1,c2:True)>>>blue=Color("blue",equality=lambdac1,c2:True)Note that these instances would compare as equal to any other color:>>>red==blueTrueBut on another non-Colour object:>>>red==NoneFalse>>>red!=NoneTrueActually,Colourinstances will, politely enough, leave
the other side of the equality have a chance to decide of the output,
(by executing its own__eq__), so:>>>classOtherColorImplem(object):...def__init__(self,color):...self.color=color...def__eq__(self,other):...returnself.color==other.web>>>alien_red=OtherColorImplem("red")>>>red==alien_redTrue>>>blue==alien_redFalseAnd inequality (using__ne__) are also polite:>>>classAnotherColorImplem(OtherColorImplem):...def__ne__(self,other):...returnself.color!=other.web>>>new_alien_red=AnotherColorImplem("red")>>>red!=new_alien_redFalse>>>blue!=new_alien_redTruePicking arbitrary color for a python objectBasic UsageSometimes, you just want to pick a color for an object in your application
often to visually identify this object. Thus, the picked color should be the
same for same objects, and different for different object:>>>foo=object()>>>bar=object()>>>Color(pick_for=foo)# doctest: +ELLIPSIS<Color...>>>>Color(pick_for=foo)==Color(pick_for=foo)True>>>Color(pick_for=foo)==Color(pick_for=bar)FalseOf course, although there’s a tiny probability that different strings yield the
same color, most of the time, different inputs will produce different colors.Advanced UsageYou can customize your color picking algorithm by providing apicker. Apickeris a callable that takes an object, and returns something that can
be instantiated as a color byColor:>>>my_picker=lambdaobj:"red"ifisinstance(obj,int)else"blue">>>Color(pick_for=3,picker=my_picker,pick_key=None)<Colorred>>>>Color(pick_for="foo",picker=my_picker,pick_key=None)<Colorblue>You might want to use a particular picker, but enforce how the picker will
identify two object as the same (or not). So there’s apick_keyattribute
that is provided and defaults as equivalent ofhashmethod and if hash is
not supported by your object, it’ll default to thestrof your object salted
with the class name.Thus:>>>classMyObj(str):pass>>>my_obj_color=Color(pick_for=MyObj("foo"))>>>my_str_color=Color(pick_for="foo")>>>my_obj_color==my_str_colorFalsePlease make sure your object is hashable or “stringable” before using theRGB_color_pickerpicking mechanism or provide another color picker. Nearly
all python object are hashable by default so this shouldn’t be an issue (e.g.
instances ofobjectand subclasses are hashable).Neitherhashnorstrare perfect solution. So feel free to usepick_keyatColorinstantiation time to set your way to identify
objects, for instance:>>>a=object()>>>b=object()>>>Color(pick_for=a,pick_key=id)==Color(pick_for=b,pick_key=id)FalseWhen choosing a pick key, you should closely consider if you want your color
to be consistent between runs (this is NOT the case with the last example),
or consistent with the content of your object if it is a mutable object.Default value ofpick_keyandpickerensures that the same color will
be attributed to same object between different run on different computer for
most python object.Color factoryAs you might have noticed, there are few attributes that you might want to see
attached to all of your colors asequalityfor equality comparison support,
orpicker,pick_keyto configure your object color picker.You can create a customizedColorfactory thanks to themake_color_factory:>>>fromcolourimportmake_color_factory,HSL_equivalence,RGB_color_picker>>>get_color=make_color_factory(...equality=HSL_equivalence,...picker=RGB_color_picker,...pick_key=str,...)All color created thanks toCustomColorclass instead of the default one
would get the specified attributes by default:>>>black_red=get_color("red",luminance=0)>>>black_blue=get_color("blue",luminance=0)Of course, these are always instances ofColorclass:>>>isinstance(black_red,Color)TrueEquality was changed from normal defaults, so:>>>black_red==black_blueFalseThis because the default equivalence ofColorwas set toHSL_equivalence.ContributingAny suggestion or issue is welcome. Push request are very welcome,
please check out the guidelines.Push Request GuidelinesYou can send any code. I’ll look at it and will integrate it myself in
the code base and leave you as the author. This process can take time and
it’ll take less time if you follow the following guidelines:check your code with PEP8 or pylint. Try to stick to 80 columns wide.separate your commits per smallest concern.each commit should pass the tests (to allow easy bisect)each functionality/bugfix commit should contain the code, tests,
and doc.prior minor commit with typographic or code cosmetic changes are
very welcome. These should be tagged in their commit summary with!minor.the commit message should follow gitchangelog rules (check the git
log to get examples)if the commit fixes an issue or finished the implementation of a
feature, please mention it in the summary.If you have some questions about guidelines which is not answered here,
please check the currentgit log, you might find previous commit that
would show you how to deal with your issue.LicenseCurrent licenseCopyright (c) 2022 Marcin Serwin.Licensed underBSD License.Original licenseThe project was forked fromColour.Copyright (c) 2012-2017 Valentin Lab.Licensed under theBSD License.