askbot.deps.grapefruit

GrapeFruit - Color manipulation in Python

class askbot.deps.grapefruit.Color(values, mode='rgb', alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Hold a color value.

Example usage:

To create an instance of the grapefruit.Color from RGB values:

>>> import grapefruit
>>> r, g, b = 1, 0.5, 0
>>> col = grapefruit.Color.NewFromRgb(r, g, b)

To get the values of the color in another colorspace:

>>> h, s, v = col.hsv
>>> l, a, b = col.lab

To get the complementary of a color:

>>> compl = col.ComplementaryColor()
>>> print compl.hsl
(210.0, 1.0, 0.5)

To directly convert RGB values to their HSL equivalent:

>>> h, s, l = Color.RgbToHsl(r, g, b)
AlphaBlend(other)

Alpha-blend this color on the other one.

Args:
other:The grapefruit.Color to alpha-blend with this one.
Returns:
A grapefruit.Color instance which is the result of alpha-blending this color on the other one.
>>> c1 = Color.NewFromRgb(1, 0.5, 0, 0.2)
>>> c2 = Color.NewFromRgb(1, 1, 1, 0.8)
>>> c3 = c1.AlphaBlend(c2)
>>> str(c3)
'(1, 0.875, 0.75, 0.84)'
AnalogousScheme(angle=30, mode='ryb')

Return two colors analogous to this one.

Args:
angle:The angle between the hues of the created colors and this one.
mode:Select which color wheel to use for the generation (ryb/rgb).
Returns:
A tuple of grapefruit.Colors analogous to this one.
>>> c1 = Color.NewFromHsl(30, 1, 0.5)
>>> c2, c3 = c1.AnalogousScheme()
>>> c2.hsl
(330, 1, 0.5)
>>> c3.hsl
(90, 1, 0.5)
>>> c2, c3 = c1.AnalogousScheme(10)
>>> c2.hsl
(20, 1, 0.5)
>>> c3.hsl
(40, 1, 0.5)
Blend(other, percent=0.5)

Blend this color with the other one.

Args:
other:the grapefruit.Color to blend with this one.
Returns:
A grapefruit.Color instance which is the result of blending this color on the other one.
>>> c1 = Color.NewFromRgb(1, 0.5, 0, 0.2)
>>> c2 = Color.NewFromRgb(1, 1, 1, 0.6)
>>> c3 = c1.Blend(c2)
>>> str(c3)
'(1, 0.75, 0.5, 0.4)'
static CmyToCmyk(c, m, y)

Convert the color from CMY coordinates to CMYK.

Parameters:
c:The Cyan component value [0...1]
m:The Magenta component value [0...1]
y:The Yellow component value [0...1]
Returns:
The color as an (c, m, y, k) tuple in the range: c[0...1], m[0...1], y[0...1], k[0...1]
>>> '(%g, %g, %g, %g)' % Color.CmyToCmyk(1, 0.66, 0.5)
'(1, 0.32, 0, 0.5)'
static CmyToRgb(c, m, y)

Convert the color from CMY coordinates to RGB.

Parameters:
c:The Cyan component value [0...1]
m:The Magenta component value [0...1]
y:The Yellow component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range: r[0...1], g[0...1], b[0...1]
>>> Color.CmyToRgb(0, 0.5, 1)
(1, 0.5, 0)
static CmykToCmy(c, m, y, k)

Convert the color from CMYK coordinates to CMY.

Parameters:
c:The Cyan component value [0...1]
m:The Magenta component value [0...1]
y:The Yellow component value [0...1]
k:The Black component value [0...1]
Returns:
The color as an (c, m, y) tuple in the range: c[0...1], m[0...1], y[0...1]
>>> '(%g, %g, %g)' % Color.CmykToCmy(1, 0.32, 0, 0.5)
'(1, 0.66, 0.5)'
ColorWithAlpha(alpha)

Create a new instance based on this one with a new alpha value.

Parameters:
alpha:The transparency of the new color [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromRgb(1.0, 0.5, 0.0, 1.0).ColorWithAlpha(0.5)
(1.0, 0.5, 0.0, 0.5)
ColorWithHue(hue)

Create a new instance based on this one with a new hue.

Parameters:
hue:The hue of the new color [0...360].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithHue(60)
(1.0, 1.0, 0.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithHue(60).hsl
(60, 1, 0.5)
ColorWithLightness(lightness)

Create a new instance based on this one with a new lightness value.

Parameters:
lightness:The lightness of the new color [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithLightness(0.25)
(0.5, 0.25, 0.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithLightness(0.25).hsl
(30, 1, 0.25)
ColorWithSaturation(saturation)

Create a new instance based on this one with a new saturation value.

Note

The saturation is defined for the HSL mode.

Parameters:
saturation:The saturation of the new color [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithSaturation(0.5)
(0.75, 0.5, 0.25, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithSaturation(0.5).hsl
(30, 0.5, 0.5)
ColorWithWhiteRef(wref, labAsRef=False)

Create a new instance based on this one with a new white reference.

Parameters:
wref:The whitepoint reference.
labAsRef:If True, the L*a*b* values of the current instance are used as reference for the new color; otherwise, the RGB values are used as reference.
Returns:
A grapefruit.Color instance.
>>> c = Color.NewFromRgb(1.0, 0.5, 0.0, 1.0, Color.WHITE_REFERENCE['std_D65'])
>>> c2 = c.ColorWithWhiteRef(Color.WHITE_REFERENCE['sup_D50'])
>>> c2.rgb
(1.0, 0.5, 0.0)
>>> '(%g, %g, %g)' % c2.whiteRef
'(0.96721, 1, 0.81428)'
>>> c2 = c.ColorWithWhiteRef(Color.WHITE_REFERENCE['sup_D50'], labAsRef=True)
>>> '(%g, %g, %g)' % c2.rgb
'(1.01463, 0.490339, -0.148131)'
>>> '(%g, %g, %g)' % c2.whiteRef
'(0.96721, 1, 0.81428)'
>>> '(%g, %g, %g)' % c.lab
'(66.9518, 0.43084, 0.739692)'
>>> '(%g, %g, %g)' % c2.lab
'(66.9518, 0.43084, 0.739693)'
ComplementaryColor(mode='ryb')

Create a new instance which is the complementary color of this one.

Parameters:
mode:Select which color wheel to use for the generation (ryb/rgb).
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).ComplementaryColor()
(0.0, 0.5, 1.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).ComplementaryColor().hsl
(210, 1, 0.5)
DarkerColor(level)

Create a new instance based on this one but darker.

Parameters:
level:The amount by which the color should be darkened to produce the new one [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).DarkerColor(0.25)
(0.5, 0.25, 0.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).DarkerColor(0.25).hsl
(30, 1, 0.25)
Desaturate(level)

Create a new instance based on this one but less saturated.

Parameters:
level:The amount by which the color should be desaturated to produce the new one [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 0.5, 0.5).Desaturate(0.25)
(0.625, 0.5, 0.375, 1.0)
>>> Color.NewFromHsl(30, 0.5, 0.5).Desaturate(0.25).hsl
(30, 0.25, 0.5)
Gradient(target, steps=100)

Create a list with the gradient colors between this and the other color.

Parameters:
target:The grapefruit.Color at the other end of the gradient.
steps:The number of gradients steps to create.
Returns:
A list of grapefruit.Color instances.
>>> c1 = Color.NewFromRgb(1.0, 0.0, 0.0, alpha=1)
>>> c2 = Color.NewFromRgb(0.0, 1.0, 0.0, alpha=0)
>>> c1.Gradient(c2, 3)
[(0.75, 0.25, 0.0, 0.75), (0.5, 0.5, 0.0, 0.5), (0.25, 0.75, 0.0, 0.25)]
static HslToRgb(h, s, l)

Convert the color from HSL coordinates to RGB.

Parameters:
h:The Hue component value [0...1]
s:The Saturation component value [0...1]
l:The Lightness component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range: r[0...1], g[0...1], b[0...1]
>>> Color.HslToRgb(30.0, 1.0, 0.5)
(1.0, 0.5, 0.0)
static HsvToRgb(h, s, v)

Convert the color from RGB coordinates to HSV.

Parameters:
h:The Hus component value [0...1]
s:The Saturation component value [0...1]
v:The Value component [0...1]
Returns:
The color as an (r, g, b) tuple in the range: r[0...1], g[0...1], b[0...1]
>>> Color.HslToRgb(30.0, 1.0, 0.5)
(1.0, 0.5, 0.0)
static HtmlToRgb(html)

Convert the HTML color to (r, g, b).

Parameters:
html:the HTML definition of the color (#RRGGBB or #RGB or a color name).
Returns:
The color as an (r, g, b) tuple in the range: r[0...1], g[0...1], b[0...1]
Throws:
ValueError:If html is neither a known color name or a hexadecimal RGB representation.
>>> '(%g, %g, %g)' % Color.HtmlToRgb('#ff8000')
'(1, 0.501961, 0)'
>>> '(%g, %g, %g)' % Color.HtmlToRgb('ff8000')
'(1, 0.501961, 0)'
>>> '(%g, %g, %g)' % Color.HtmlToRgb('#f60')
'(1, 0.4, 0)'
>>> '(%g, %g, %g)' % Color.HtmlToRgb('f60')
'(1, 0.4, 0)'
>>> '(%g, %g, %g)' % Color.HtmlToRgb('lemonchiffon')
'(1, 0.980392, 0.803922)'
static LabToXyz(l, a, b, wref=(0.95043, 1.0, 1.0889))

Convert the color from CIE L*a*b* to CIE 1931 XYZ.

Parameters:
l:The L component [0...100]
a:The a component [-1...1]
b:The a component [-1...1]
wref:The whitepoint reference, default is 2° D65.
Returns:
The color as an (x, y, z) tuple in the range: x[0...q], y[0...1], z[0...1]
>>> '(%g, %g, %g)' % Color.LabToXyz(66.9518, 0.43084, 0.739692)
'(0.488941, 0.365682, 0.0448137)'
>>> '(%g, %g, %g)' % Color.LabToXyz(66.9518, 0.411663, 0.67282, Color.WHITE_REFERENCE['std_D50'])
'(0.488941, 0.365682, 0.0448138)'
LighterColor(level)

Create a new instance based on this one but lighter.

Parameters:
level:The amount by which the color should be lightened to produce the new one [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).LighterColor(0.25)
(1.0, 0.75, 0.5, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).LighterColor(0.25).hsl
(30, 1, 0.75)
MonochromeScheme()

Return 4 colors in the same hue with varying saturation/lightness.

Returns:
A tuple of 4 grapefruit.Color in the same hue as this one, with varying saturation/lightness.
>>> c = Color.NewFromHsl(30, 0.5, 0.5)
>>> ['(%g, %g, %g)' % clr.hsl for clr in c.MonochromeScheme()]
['(30, 0.2, 0.8)', '(30, 0.5, 0.3)', '(30, 0.2, 0.6)', '(30, 0.5, 0.8)']
static NewFromCmy(c, m, y, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed CMY values.

Parameters:
c:The Cyan component value [0...1]
m:The Magenta component value [0...1]
y:The Yellow component value [0...1]
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> Color.NewFromCmy(0, 0.5, 1)
(1, 0.5, 0, 1.0)
>>> Color.NewFromCmy(0, 0.5, 1, 0.5)
(1, 0.5, 0, 0.5)
static NewFromCmyk(c, m, y, k, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed CMYK values.

Parameters:
c:The Cyan component value [0...1]
m:The Magenta component value [0...1]
y:The Yellow component value [0...1]
k:The Black component value [0...1]
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromCmyk(1, 0.32, 0, 0.5))
'(0, 0.34, 0.5, 1)'
>>> str(Color.NewFromCmyk(1, 0.32, 0, 0.5, 0.5))
'(0, 0.34, 0.5, 0.5)'
static NewFromHsl(h, s, l, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed HSL values.

Parameters:
h:The Hue component value [0...1]
s:The Saturation component value [0...1]
l:The Lightness component value [0...1]
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5)
(1.0, 0.5, 0.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5, 0.5)
(1.0, 0.5, 0.0, 0.5)
static NewFromHsv(h, s, v, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed HSV values.

Parameters:
h:The Hus component value [0...1]
s:The Saturation component value [0...1]
v:The Value component [0...1]
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsv(30, 1, 1)
(1.0, 0.5, 0.0, 1.0)
>>> Color.NewFromHsv(30, 1, 1, 0.5)
(1.0, 0.5, 0.0, 0.5)
static NewFromHtml(html, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed HTML color definition.

Parameters:
html:The HTML definition of the color (#RRGGBB or #RGB or a color name).
alpha:The color transparency [0...1], default is opaque.
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromHtml('#ff8000'))
'(1, 0.501961, 0, 1)'
>>> str(Color.NewFromHtml('ff8000'))
'(1, 0.501961, 0, 1)'
>>> str(Color.NewFromHtml('#f60'))
'(1, 0.4, 0, 1)'
>>> str(Color.NewFromHtml('f60'))
'(1, 0.4, 0, 1)'
>>> str(Color.NewFromHtml('lemonchiffon'))
'(1, 0.980392, 0.803922, 1)'
>>> str(Color.NewFromHtml('#ff8000', 0.5))
'(1, 0.501961, 0, 0.5)'
static NewFromLab(l, a, b, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed CIE-LAB values.

Parameters:
l:The L component [0...100]
a:The a component [-1...1]
b:The a component [-1...1]
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromLab(66.9518, 0.43084, 0.739692))
'(1, 0.5, 1.09491e-008, 1)'
>>> str(Color.NewFromLab(66.9518, 0.43084, 0.739692, wref=Color.WHITE_REFERENCE['std_D50']))
'(1.01238, 0.492011, -0.14311, 1)'
>>> str(Color.NewFromLab(66.9518, 0.43084, 0.739692, 0.5))
'(1, 0.5, 1.09491e-008, 0.5)'
>>> str(Color.NewFromLab(66.9518, 0.43084, 0.739692, 0.5, Color.WHITE_REFERENCE['std_D50']))
'(1.01238, 0.492011, -0.14311, 0.5)'
static NewFromPil(pil, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed PIL color.

Parameters:
pil:A PIL compatible color representation (0xBBGGRR)
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromPil(0x0080ff))
'(1, 0.501961, 0, 1)'
>>> str(Color.NewFromPil(0x0080ff, 0.5))
'(1, 0.501961, 0, 0.5)'
static NewFromRgb(r, g, b, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed RGB values.

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> Color.NewFromRgb(1.0, 0.5, 0.0)
(1.0, 0.5, 0.0, 1.0)
>>> Color.NewFromRgb(1.0, 0.5, 0.0, 0.5)
(1.0, 0.5, 0.0, 0.5)
static NewFromXyz(x, y, z, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed CIE-XYZ values.

Parameters:
x:The Red component value [0...1]
y:The Green component value [0...1]
z:The Blue component value [0...1]
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromXyz(0.488941, 0.365682, 0.0448137))
'(1, 0.5, 6.81883e-008, 1)'
>>> str(Color.NewFromXyz(0.488941, 0.365682, 0.0448137, 0.5))
'(1, 0.5, 6.81883e-008, 0.5)'
static NewFromYiq(y, i, q, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed YIQ values.

Parameters:
y:The Y component value [0...1]
i:The I component value [0...1]
q:The Q component value [0...1]
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromYiq(0.5922, 0.45885,-0.05))
'(0.999902, 0.499955, -6.6905e-005, 1)'
>>> str(Color.NewFromYiq(0.5922, 0.45885,-0.05, 0.5))
'(0.999902, 0.499955, -6.6905e-005, 0.5)'
static NewFromYuv(y, u, v, alpha=1.0, wref=(0.95043, 1.0, 1.0889))

Create a new instance based on the specifed YUV values.

Parameters:
y:The Y component value [0...1]
u:The U component value [-0.436...0.436]
v:The V component value [-0.615...0.615]
alpha:The color transparency [0...1], default is opaque
wref:The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromYuv(0.5925, -0.2916, 0.3575))
'(0.999989, 0.500015, -6.3276e-005, 1)'
>>> str(Color.NewFromYuv(0.5925, -0.2916, 0.3575, 0.5))
'(0.999989, 0.500015, -6.3276e-005, 0.5)'
static PilToRgb(pil)

Convert the color from a PIL-compatible integer to RGB.

Parameters:
pil: a PIL compatible color representation (0xBBGGRR)
Returns:
The color as an (r, g, b) tuple in the range: the range: r: [0...1] g: [0...1] b: [0...1]
>>> '(%g, %g, %g)' % Color.PilToRgb(0x0080ff)
'(1, 0.501961, 0)'
static RgbToCmy(r, g, b)

Convert the color from RGB coordinates to CMY.

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
Returns:
The color as an (c, m, y) tuple in the range: c[0...1], m[0...1], y[0...1]
>>> Color.RgbToCmy(1, 0.5, 0)
(0, 0.5, 1)
static RgbToGreyscale(r, g, b)

Convert the color from RGB to its greyscale equivalent

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range: the range: r[0...1], g[0...1], b[0...1]
>>> '(%g, %g, %g)' % Color.RgbToGreyscale(1, 0.8, 0)
'(0.6, 0.6, 0.6)'
static RgbToHsl(r, g, b)

Convert the color from RGB coordinates to HSL.

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
Returns:
The color as an (h, s, l) tuple in the range: h[0...360], s[0...1], l[0...1]
>>> Color.RgbToHsl(1, 0.5, 0)
(30.0, 1.0, 0.5)
static RgbToHsv(r, g, b)

Convert the color from RGB coordinates to HSV.

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
Returns:
The color as an (h, s, v) tuple in the range: h[0...360], s[0...1], v[0...1]
>>> Color.RgbToHsv(1, 0.5, 0)
(30.0, 1, 1)
static RgbToHtml(r, g, b)

Convert the color from (r, g, b) to #RRGGBB.

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
Returns:
A CSS string representation of this color (#RRGGBB).
>>> Color.RgbToHtml(1, 0.5, 0)
'#ff8000'
static RgbToPil(r, g, b)

Convert the color from RGB to a PIL-compatible integer.

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
Returns:
A PIL compatible integer (0xBBGGRR).
>>> '0x%06x' % Color.RgbToPil(1, 0.5, 0)
'0x0080ff'
static RgbToRyb(hue)

Maps a hue on the RGB color wheel to Itten’s RYB wheel.

Parameters:
hue:The hue on the RGB color wheel [0...360]
Returns:
An approximation of the corresponding hue on Itten’s RYB wheel.
>>> Color.RgbToRyb(15)
26
static RgbToWebSafe(r, g, b, alt=False)

Convert the color from RGB to ‘web safe’ RGB

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
alt:If True, use the alternative color instead of the nearest one. Can be used for dithering.
Returns:
The color as an (r, g, b) tuple in the range: the range: r[0...1], g[0...1], b[0...1]
>>> '(%g, %g, %g)' % Color.RgbToWebSafe(1, 0.55, 0.0)
'(1, 0.6, 0)'
static RgbToXyz(r, g, b)

Convert the color from sRGB to CIE XYZ.

The methods assumes that the RGB coordinates are given in the sRGB colorspace (D65).

Note

Compensation for the sRGB gamma correction is applied before converting.

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
Returns:
The color as an (x, y, z) tuple in the range: x[0...1], y[0...1], z[0...1]
>>> '(%g, %g, %g)' % Color.RgbToXyz(1, 0.5, 0)
'(0.488941, 0.365682, 0.0448137)'
static RgbToYiq(r, g, b)

Convert the color from RGB to YIQ.

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
Returns:
The color as an (y, i, q) tuple in the range: y[0...1], i[0...1], q[0...1]
>>> '(%g, %g, %g)' % Color.RgbToYiq(1, 0.5, 0)
'(0.592263, 0.458874, -0.0499818)'
static RgbToYuv(r, g, b)

Convert the color from RGB coordinates to YUV.

Parameters:
r:The Red component value [0...1]
g:The Green component value [0...1]
b:The Blue component value [0...1]
Returns:
The color as an (y, u, v) tuple in the range: y[0...1], u[-0.436...0.436], v[-0.615...0.615]
>>> '(%g, %g, %g)' % Color.RgbToYuv(1, 0.5, 0)
'(0.5925, -0.29156, 0.357505)'
static RybToRgb(hue)

Maps a hue on Itten’s RYB color wheel to the standard RGB wheel.

Parameters:
hue:The hue on Itten’s RYB color wheel [0...360]
Returns:
An approximation of the corresponding hue on the standard RGB wheel.
>>> Color.RybToRgb(15)
8
Saturate(level)

Create a new instance based on this one but more saturated.

Parameters:
level:The amount by which the color should be saturated to produce the new one [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 0.5, 0.5).Saturate(0.25)
(0.875, 0.5, 0.125, 1.0)
>>> Color.NewFromHsl(30, 0.5, 0.5).Saturate(0.25).hsl
(30, 0.75, 0.5)
TetradicScheme(angle=30, mode='ryb')

Return three colors froming a tetrad with this one.

Parameters:
angle:The angle to substract from the adjacent colors hues [-90...90]. You can use an angle of zero to generate a square tetrad.
mode:Select which color wheel to use for the generation (ryb/rgb).
Returns:
A tuple of three grapefruit.Color forming a color tetrad with this one.
>>> col = Color.NewFromHsl(30, 1, 0.5)
>>> [c.hsl for c in col.TetradicScheme(mode='rgb', angle=30)]
[(90, 1, 0.5), (210, 1, 0.5), (270, 1, 0.5)]
TriadicScheme(angle=120, mode='ryb')

Return two colors forming a triad or a split complementary with this one.

Parameters:
angle:The angle between the hues of the created colors. The default value makes a triad.
mode:Select which color wheel to use for the generation (ryb/rgb).
Returns:
A tuple of two grapefruit.Color forming a color triad with this one or a split complementary.
>>> c1 = Color.NewFromHsl(30, 1, 0.5)
>>> c2, c3 = c1.TriadicScheme()
>>> c2.hsl
(150.0, 1, 0.5)
>>> c3.hsl
(270.0, 1, 0.5)
>>> c2, c3 = c1.TriadicScheme(40)
>>> c2.hsl
(190.0, 1, 0.5)
>>> c3.hsl
(230.0, 1, 0.5)
WebSafeDither()

Return the two websafe colors nearest to this one.

Returns:
A tuple of two grapefruit.Color instances which are the two web safe colors closest this one.
>>> c = Color.NewFromRgb(1.0, 0.45, 0.0)
>>> c1, c2 = c.WebSafeDither()
>>> str(c1)
'(1, 0.4, 0, 1)'
>>> str(c2)
'(1, 0.6, 0, 1)'
static XyzToLab(x, y, z, wref=(0.95043, 1.0, 1.0889))

Convert the color from CIE XYZ to CIE L*a*b*.

Parameters:
x:The X component value [0...1]
y:The Y component value [0...1]
z:The Z component value [0...1]
wref:The whitepoint reference, default is 2° D65.
Returns:
The color as an (L, a, b) tuple in the range: L[0...100], a[-1...1], b[-1...1]
>>> '(%g, %g, %g)' % Color.XyzToLab(0.488941, 0.365682, 0.0448137)
'(66.9518, 0.43084, 0.739692)'
>>> '(%g, %g, %g)' % Color.XyzToLab(0.488941, 0.365682, 0.0448137, Color.WHITE_REFERENCE['std_D50'])
'(66.9518, 0.411663, 0.67282)'
static XyzToRgb(x, y, z)

Convert the color from CIE XYZ coordinates to sRGB.

Note

Compensation for sRGB gamma correction is applied before converting.

Parameters:
x:The X component value [0...1]
y:The Y component value [0...1]
z:The Z component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range: r[0...1], g[0...1], b[0...1]
>>> '(%g, %g, %g)' % Color.XyzToRgb(0.488941, 0.365682, 0.0448137)
'(1, 0.5, 6.81883e-008)'
static YiqToRgb(y, i, q)

Convert the color from YIQ coordinates to RGB.

Parameters:
y:Tte Y component value [0...1]
i:The I component value [0...1]
q:The Q component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range: r[0...1], g[0...1], b[0...1]
>>> '(%g, %g, %g)' % Color.YiqToRgb(0.592263, 0.458874, -0.0499818)
'(1, 0.5, 5.442e-007)'
static YuvToRgb(y, u, v)

Convert the color from YUV coordinates to RGB.

Parameters:
y:The Y component value [0...1]
u:The U component value [-0.436...0.436]
v:The V component value [-0.615...0.615]
Returns:
The color as an (r, g, b) tuple in the range: r[0...1], g[0...1], b[0...1]
>>> '(%g, %g, %g)' % Color.YuvToRgb(0.5925, -0.2916, 0.3575)
'(0.999989, 0.500015, -6.3276e-005)'
alpha

The transparency of this color. 0.0 is transparent and 1.0 is fully opaque.

cmy

The CMY values of this Color.

cmyk

The CMYK values of this Color.

greyscale

The greyscale equivalent to this color (RGB).

hsl

The HSL values of this Color.

hsv

The HSV values of this Color.

html

This Color as an HTML color definition.

hue

The hue of this color.

lab

The CIE-LAB values of this Color.

pil

This Color as a PIL compatible value.

rgb

The RGB values of this Color.

webSafe

The web safe color nearest to this one (RGB).

whiteRef

the white reference point of this color.

xyz

The CIE-XYZ values of this Color.

yiq

The YIQ values of this Color.

yuv

The YUV values of this Color.

This Page