colour.color.Color¶
- class colour.color.Color(color: _Web | Color | None = None, pick_for: object | None = None, picker: Callable[[object], Color] = <function RGB_color_picker>, pick_key: Callable[[object], str | int] = <function hash_or_str>, **kwargs: Any)¶
Abstraction of a color object.
Color object keeps information of a color. It can input/output to different format (HSL, RGB, HEX, WEB) and their partial representation.
>>> from colour import Color, HSL
>>> b = Color() >>> b.hsl = HSL.BLUE
>>> b.hue 0.66... >>> b.saturation 1.0 >>> b.luminance 0.5
>>> b.red 0.0 >>> b.blue 1.0 >>> b.green 0.0
>>> b.rgb (0.0, 0.0, 1.0) >>> b.hsl (0.66..., 1.0, 0.5) >>> b.hex '#00f'
Let’s change Hue toward red tint:
>>> b.hue = 0.0 >>> b.hex '#f00'
>>> b.hue = 2.0/3 >>> b.hex '#00f'
In the other way round:
>>> b.hex = '#f00' >>> b.hsl (0.0, 1.0, 0.5)
Long hex can be accessed directly:
>>> b.hex_l = '#123456' >>> b.hex_l '#123456' >>> b.hex '#123456'
>>> b.hex_l = '#ff0000' >>> b.hex_l '#ff0000' >>> b.hex '#f00'
>>> c = Color('blue') >>> c <Color blue> >>> c.hue = 0 >>> c <Color red>
>>> c.saturation = 0.0 >>> c.hsl (..., 0.0, 0.5) >>> c.rgb (0.5, 0.5, 0.5) >>> c.hex '#7f7f7f' >>> c <Color #7f7f7f>
>>> c.luminance = 0.0 >>> c <Color black>
>>> c.hex '#000'
>>> c.green = 1.0 >>> c.blue = 1.0 >>> c.hex '#0ff' >>> c <Color cyan>
>>> c = Color('blue', luminance=0.75) >>> c <Color #7f7fff>
>>> c = Color('red', red=0.5) >>> c <Color #7f0000>
>>> print(c) #7f0000
You can try to query unexisting attributes:
>>> c.lightness Traceback (most recent call last): ... AttributeError: 'lightness' not found
TODO: could add HSV, CMYK, YUV conversion.
# >>> b.hsv # >>> b.value # >>> b.cyan # >>> b.magenta # >>> b.yellow # >>> b.key # >>> b.cmyk
To support blind conversion of web strings (or already converted object), the Color object supports instantiation with another Color object.
>>> Color(Color(Color('red'))) <Color red>
Default equality is RGB hex comparison:
>>> Color('red') == Color('blue') False >>> Color('red') == Color('red') True >>> Color('red') != Color('blue') True >>> Color('red') != Color('red') False
But this can be changed:
>>> saturation_equality = lambda c1, c2: c1.luminance == c2.luminance >>> Color('red', equality=saturation_equality) == Color('blue') True
You should be able to subclass
Colorobject without any issues:>>> class Tint(Color): ... pass
And keep the internal API working:
>>> Tint("red").hsl (0.0, 1.0, 0.5)
- __init__(color: _Web | Color | None = None, pick_for: object | None = None, picker: Callable[[object], Color] = <function RGB_color_picker>, pick_key: Callable[[object], str | int] = <function hash_or_str>, **kwargs: Any) None¶
Create
Color.- color
The initializer of color. If left as
None, it defaults to black.- pick_for
The object to be hashed and converted into color.
- picker
Method converting hash of an object into RGB color.
- pick_key
Method picking a key from object to be hashed.
Methods
__init__([color, pick_for, picker, pick_key])Create
Color.equality(c1, c2)Compare whether two colors are equivalent according to their RGB values.
from_hex(hex_value)Return new
Colorobject based on provided hex vaue.from_hsl(hsl_value)Return new
Colorobject based on provided hsl vaue.from_rgb(rgb_value)Return new
Colorobject based on provided rgb vaue.from_web(web_value)Return new
Colorobject based on provided web vaue.get_blue()Get blue component of the color.
Get green component of the color.
get_hex()Get shortest possible hex notation of the color.
Get shortest possible hex notation of the color.
get_hsl()Get hsl notation of the color.
get_hue()Get hue of the color.
Get luminance of the color.
get_red()Get red component of the color.
get_rgb()Get rgb notation of the color.
Get saturation of the color.
get_web()Get web representation of the color.
range_to(value, steps)Produce a uniform range of colors.
set_blue(value)Set blue coordinate of the color.
set_green(value)Set green coordinate of the color.
set_hex(value)Set a color using hex notation.
set_hex_l(value)Set a color using hex notation.
set_hsl(value)Set color using hsl notation.
set_hue(value)Set hue of the color.
set_luminance(value)Set luminance of the color.
set_red(value)Set red coordinate of the color.
set_rgb(value)Set color using rgb notation.
set_saturation(value)Set saturation of the color.
set_web(value)Set a color using web notation.
Attributes
Get blue of the color.
Get green of the color.
Get hex of the color.
Get long hex notation of the color.
Get hsl notation of the color.
Get hue of the color.
Get luminance of the color.
Get red of the color.
Get rgb notation of the color.
Get saturation of the color.
Get web notation of the color.
- property blue: float¶
Get blue of the color.
- static equality(c1: Color, c2: Color) bool¶
Compare whether two colors are equivalent according to their RGB values.
- static from_hex(hex_value: str) colour.color.Color¶
Return new
Colorobject based on provided hex vaue.
- static from_hsl(hsl_value: Tuple[float, float, float]) colour.color.Color¶
Return new
Colorobject based on provided hsl vaue.
- static from_rgb(rgb_value: Tuple[float, float, float]) colour.color.Color¶
Return new
Colorobject based on provided rgb vaue.
- static from_web(web_value: str) colour.color.Color¶
Return new
Colorobject based on provided web vaue.
- get_blue() float¶
Get blue component of the color.
- get_green() float¶
Get green component of the color.
- get_hex() str¶
Get shortest possible hex notation of the color.
- get_hex_l() str¶
Get shortest possible hex notation of the color.
- get_hsl() Tuple[float, float, float]¶
Get hsl notation of the color.
- get_hue() float¶
Get hue of the color.
- get_luminance() float¶
Get luminance of the color.
- get_red() float¶
Get red component of the color.
- get_rgb() Tuple[float, float, float]¶
Get rgb notation of the color.
- get_saturation() float¶
Get saturation of the color.
- get_web() str¶
Get web representation of the color.
- property green: float¶
Get green of the color.
- property hex: str¶
Get hex of the color.
- property hex_l: str¶
Get long hex notation of the color.
- property hsl: Tuple[float, float, float]¶
Get hsl notation of the color.
- property hue: float¶
Get hue of the color.
- property luminance: float¶
Get luminance of the color.
- range_to(value: colour.color.Color, steps: int) Iterator[colour.color.Color]¶
Produce a uniform range of colors.
starting with
selfand ending withvalue.
- property red: float¶
Get red of the color.
- property rgb: Tuple[float, float, float]¶
Get rgb notation of the color.
- property saturation: float¶
Get saturation of the color.
- set_blue(value: float) None¶
Set blue coordinate of the color.
- set_green(value: float) None¶
Set green coordinate of the color.
- set_hex(value: str) None¶
Set a color using hex notation.
- set_hex_l(value: str) None¶
Set a color using hex notation.
- set_hsl(value: Tuple[float, float, float]) None¶
Set color using hsl notation.
- set_hue(value: float) None¶
Set hue of the color.
- set_luminance(value: float) None¶
Set luminance of the color.
- set_red(value: float) None¶
Set red coordinate of the color.
- set_rgb(value: Tuple[float, float, float]) None¶
Set color using rgb notation.
- set_saturation(value: float) None¶
Set saturation of the color.
- set_web(value: str) None¶
Set a color using web notation.
- property web: str¶
Get web notation of the color.