Goulib.colors module

color conversion in various colorspaces and palettes

Goulib.colors.rgb2hex(c, illuminant='ignore')[source]
Goulib.colors.hex2rgb(c, illuminant='ignore')[source]
Goulib.colors.rgb2cmyk(rgb, **kwargs)[source]
Parameters:rgb – 3-tuple of floats of red,green,blue in [0..1] range
Returns:4-tuple of floats (cyan, magenta, yellow, black) in [0..1] range
Goulib.colors.cmyk2rgb(cmyk, **kwargs)[source]
Parameters:cmyk – 4-tuple of floats (cyan, magenta, yellow, black) in [0..1] range
Result:3-tuple of floats (red,green,blue)

warning : rgb is out the [0..1] range for some cmyk

Goulib.colors.xyz2xyy(xyz, **kwargs)[source]

Convert from XYZ to xyY

Based on formula from http://brucelindbloom.com/Eqn_XYZ_to_xyY.html

Implementation Notes: 1. Watch out for black, where X = Y = Z = 0. In that case, x and y are set

to the chromaticity coordinates of the reference whitepoint.
  1. The output Y value is in the nominal range [0.0, Y[XYZ]].
Goulib.colors.xyy2xyz(xyY, **kwargs)[source]

Convert from xyY to XYZ to

Based on formula from http://brucelindbloom.com/Eqn_xyY_to_XYZ.html

Implementation Notes:

  1. Watch out for the case where y = 0. In that case, you may want to set X = Y = Z = 0.
  2. The output XYZ values are in the nominal range [0.0, 1.0].
Goulib.colors.converter(c, illuminant='ignore')
Goulib.colors.convert(color, source, target)[source]

convert a color between colorspaces, eventually using intermediary steps

class Goulib.colors.Color(value, space='RGB', name=None, illuminant='D65')[source]

Bases: object

A color with math operations and conversions Color is immutable (._values caches representations)

constructor :param value: string color name, hex string, or values tuple :param space: string defining the color space of value :param name: string for color name :param illuminant: string in {“A”, “D50”, “D55”, “D65”, “D75”, “E”}

__init__(value, space='RGB', name=None, illuminant='D65')[source]

constructor :param value: string color name, hex string, or values tuple :param space: string defining the color space of value :param name: string for color name :param illuminant: string in {“A”, “D50”, “D55”, “D65”, “D75”, “E”}

name
convert(target, **kwargs)[source]
Parameters:target – str of desired colorspace, or none for default
Returns:color in target colorspace
str(mode=None)[source]
native
rgb
hex
lab
luv
cmyk
hsv
xyz
xyY
__hash__()[source]
__repr__()[source]
compose(other, f, mode='rgb')[source]

compose colors in given mode

__add__(other)[source]
__radd__(other)[source]

only to allow sum(colors) easily

__sub__(other)[source]
__mul__(factor)[source]
__neg__()[source]

complementary color

deltaE(other)[source]

color difference according to CIEDE2000 https://en.wikipedia.org/wiki/Color_difference

isclose(other, abs_tol=1)[source]

http://zschuessler.github.io/DeltaE/learn/ <= 1.0 Not perceptible by human eyes. 1 - 2 Perceptible through close observation. 2 - 10 Perceptible at a glance. 11 - 49 Colors are more similar than opposite 100 Colors are exact opposite

__eq__(other)[source]
__class__

alias of type

__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__le__

Return self<=value.

__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__

Return str(self).

class Goulib.colors.Palette(data=[], keys=256)[source]

Bases: collections.OrderedDict

dict of Colors indexed by anything

__init__(data=[], keys=256)[source]
update(data, keys=256)[source]

updates the dictionary with new colors :param data: colors to add :param keys: keys to use in dict, or int to discretize the Colormap

index(c, dE=5)[source]
Returns:key of c or nearest color, None if distance is larger than deltaE
__repr__()[source]
patches(wide=64, size=(16, 16))[source]

Image made of each palette color

pil
Returns:a sequence of integers, or a string containing a binary

representation of the palette. In both cases, the palette contents should be ordered (r, g, b, r, g, b, …). The palette can contain up to 768 entries (3*256). If a shorter palette is given, it is padded with zeros. #http://effbot.org/zone/creating-palette-images.htm

sorted(key=<function Palette.<lambda>>)[source]
__class__

alias of type

__contains__()

True if D has a key k, else False.

__delattr__

Implement delattr(self, name).

__delitem__

Delete self[key].

__dir__() → list

default dir() implementation

__eq__

Return self==value.

__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__getitem__()

x.__getitem__(y) <==> x[y]

__gt__

Return self>value.

__hash__ = None
__iter__

Implement iter(self).

__le__

Return self<=value.

__len__

Return len(self).

__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

Return state information for pickling

__reduce_ex__()

helper for pickle

__reversed__() <==> reversed(od)
__setattr__

Implement setattr(self, name, value).

__setitem__

Set self[key] to value.

__sizeof__()
__str__

Return str(self).

clear() → None. Remove all items from od.
copy() → a shallow copy of od
fromkeys(S[, v]) → New ordered dictionary with keys from S.

If not specified, the value defaults to None.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items()
keys()
move_to_end()

Move an existing element to the end (or beginning if last==False).

Raises KeyError if the element does not exist. When last=True, acts like a fast version of self[key]=self.pop(key).

pop(k[, d]) → v, remove specified key and return the corresponding

value. If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), return and remove a (key, value) pair.

Pairs are returned in LIFO order if last is true or FIFO order if false.

setdefault(k[, d]) → od.get(k,d), also set od[k]=d if k not in od
values()
Goulib.colors.ColorTable(colors, key=None, width=10)[source]
Goulib.colors.color_to_aci(x, nearest=True)[source]
Returns:int Autocad Color Index of color x
Goulib.colors.aci_to_color(x, block_color=None, layer_color=None)[source]
Goulib.colors.deltaE(c1, c2)[source]
Goulib.colors.nearest_color(c, l=None, opt=<built-in function min>, comp=<function deltaE>)[source]
Parameters:
  • x – Color
  • l – list or dict of Color, color by default
  • opt – with opt=max you can find the most different color …
Returns:

nearest Color of x in l

Goulib.colors.color_range(n, start, end, space='hsv')[source]
Parameters:
  • n – int number of colors to generate
  • start – string hex color or color name
  • end – string hex color or color name
Result:

list of n Color interpolated between start and end, included