- Color Conversions¶
- Example¶
- RGB conversions and native illuminants¶
- RGB conversions and out-of-gamut coordinates¶
- colorsys — Conversions between color systems¶
- Saved searches
- Use saved searches to filter your results more quickly
- License
- senritsuki/Python-color-converter
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- mathebox / color_conversion.py
Color Conversions¶
Converting between color spaces is very simple with python-colormath. To see a full list of supported color spaces, see Color Objects .
All conversions happen through the convert_color function shown below. The original Color instance is passed in as the first argument, and the desired Color class (not an instance) is passed in as the second argument. If the conversion can be made, a new Color instance will be returned.
colormath.color_conversions. convert_color ( color, target_cs, through_rgb_type= , target_illuminant=None, *args, **kwargs ) [source] ¶
Converts the color to the designated color space.
- color – A Color instance to convert.
- target_cs – The Color class to convert to. Note that this is not an instance, but a class.
- through_rgb_type (BaseRGBColor) – If during your conversion between your original and target color spaces you have to pass through RGB, this determines which kind of RGB to use. For example, XYZ->HSL. You probably don’t need to specify this unless you have a special usage case.
- target_illuminant (Noneorstr) – If during conversion from RGB to a reflective color space you want to explicitly end up with a certain illuminant, pass this here. Otherwise the RGB space’s native illuminant will be used.
An instance of the type passed in as target_cs .
colormath.color_exceptions.UndefinedConversionError if conversion between the two color spaces isn’t possible.
Example¶
This is a simple example of a CIE Lab to CIE XYZ conversion. Refer to Color Objects for a full list of different color spaces that can be instantiated and converted between.
from colormath.color_objects import LabColor, XYZColor from colormath.color_conversions import convert_color lab = LabColor(0.903, 16.296, -2.22) xyz = convert_color(lab, XYZColor)
Some color spaces require a trip through RGB during conversion. For example, to get from XYZ to HSL, we have to convert XYZ->RGB->HSL. The same could be said for XYZ to CMYK (XYZ->RGB->CMY->CMYK). Different RGB color spaces have different gamut sizes and capabilities, which can affect your converted color values.
sRGB is the default RGB color space due to its ubiquity. If you would like to use a different RGB space for a conversion, you can do something like this:
from colormath.color_objects import XYZColor, HSLColor, AdobeRGBColor from colormath.color_conversions import convert_color xyz = XYZColor(0.1, 0.2, 0.3) hsl = convert_color(xyz, HSLColor, through_rgb_type=AdobeRGBColor) # If you are going to convert back to XYZ, make sure you use the same # RGB color space on the way back. xyz2 = convert_color(hsl, XYZColor, through_rgb_type=AdobeRGBColor)
RGB conversions and native illuminants¶
When converting RGB colors to any of the CIE spaces, we have to pass through the XYZ color space. This serves as a crossroads for conversions to basically all of the reflective color spaces (CIE Lab, LCH, Luv, etc). The RGB spaces are reflective, where the illumination is provided. In the case of a reflective space like XYZ, the illuminant must be supplied by a light source.
Each RGB space has its own native illuminant, which can vary from space to space. To see some of these for yourself, check out Bruce Lindbloom’s XYZ to RGB matrices.
To cite the most commonly used RGB color space as an example, sRGB has a native illuminant of D65. When we convert RGB to XYZ, that native illuminant carries over unless explicitly overridden. If you aren’t expecting this behavior, you’ll end up with variations in your converted color’s numbers.
To explicitly request a specific illuminant, provide the target_illuminant keyword when using colormath.color_conversions.convert_color() .
from colormath.color_objects import XYZColor, sRGBColor from colormath.color_conversions import convert_color rgb = RGBColor(0.1, 0.2, 0.3) xyz = convert_color(rgb, XYZColor, target_illuminant='d50')
RGB conversions and out-of-gamut coordinates¶
RGB spaces tend to have a smaller gamut than some of the CIE color spaces. When converting to RGB, this can cause some of the coordinates to end up being out of the acceptable range (0.0-1.0 or 1-255, depending on whether your RGB color is upscaled).
Rather than clamp these for you, we leave them as-is. This allows for more accurate conversions back to the CIE color spaces. If you require the clamped (0.0-1.0 or 1-255) values, use the following properties on any RGB color:
© Copyright 2014, Greg Taylor. Revision 1d168613 .
Versions latest stable 3.0.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 master Downloads pdf htmlzip epub On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.
colorsys — Conversions between color systems¶
The colorsys module defines bidirectional conversions of color values between colors expressed in the RGB (Red Green Blue) color space used in computer monitors and three other coordinate systems: YIQ, HLS (Hue Lightness Saturation) and HSV (Hue Saturation Value). Coordinates in all of these color spaces are floating point values. In the YIQ space, the Y coordinate is between 0 and 1, but the I and Q coordinates can be positive or negative. In all other spaces, the coordinates are all between 0 and 1.
The colorsys module defines the following functions:
Convert the color from RGB coordinates to YIQ coordinates.
Convert the color from YIQ coordinates to RGB coordinates.
Convert the color from RGB coordinates to HLS coordinates.
Convert the color from HLS coordinates to RGB coordinates.
Convert the color from RGB coordinates to HSV coordinates.
Convert the color from HSV coordinates to RGB coordinates.
>>> import colorsys >>> colorsys.rgb_to_hsv(0.2, 0.4, 0.4) (0.5, 0.5, 0.4) >>> colorsys.hsv_to_rgb(0.5, 0.5, 0.4) (0.2, 0.4, 0.4)
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Color converter for Python 3.x. Supported formats: RGB, HSL, XYZ, L*a*b*, L*C*h.
License
senritsuki/Python-color-converter
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Color converter for Python 3.x. Supported formats: RGB, HSL, XYZ, L*a*b*, L*C*h.
import color_converter as cc cc.rgbhex_to_rgb255('#0080ff') # -> [0, 128, 255] cc.rgb255_to_rgbhex((0, 128, 255)) # -> '#0080ff'
Function name | Argument value | Return value |
---|---|---|
hsl_to_rgb01 | HSL array (0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0) | RGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) |
hsl_to_rgb255 | HSL array (0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0) | RGB array (0, 0, 0) .. (255, 255, 255) |
hsl_to_rgbhex | HSL array (0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0) | RGB string ‘#000000’ .. ‘#ffffff’ |
lab_to_lch | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) | D50 CIE L*C*h array (0.0, 0.0, 0.0) .. (100.0, *.*, 360.0) |
lab_to_rgb01 | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) | RGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) |
lab_to_rgb255 | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) | RGB array (0, 0, 0) .. (255, 255, 255) |
lab_to_rgbhex | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) | RGB string ‘#000000’ .. ‘#ffffff’ |
lab_to_xyz | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) | D50 CIE XYZ array (*.*, *.*, *.*) .. (*.*, *.*, *.*) |
lch_to_lab | D50 CIE L*C*h array (0.0, 0.0, 0.0) .. (100.0, *.*, 360.0) | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) |
lch_to_rgb255 | D50 CIE L*C*h array (0.0, 0.0, 0.0) .. (100.0, *.*, 360.0) | RGB array (0, 0, 0) .. (255, 255, 255) |
lch_to_rgbhex | D50 CIE L*C*h array (0.0, 0.0, 0.0) .. (100.0, *.*, 360.0) | RGB string ‘#000000’ .. ‘#ffffff’ |
lrgb_to_srgb | D65 linear sRGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) | D65 non linear sRGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) |
lrgb_to_xyz | D65 linear sRGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) | D50 CIE XYZ array (*.*, *.*, *.*) .. (*.*, *.*, *.*) |
rgb01_to_hsl | RGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) | HSL array (0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0) |
rgb01_to_lab | RGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) |
rgb01_to_rgb255 | RGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) | RGB array (0, 0, 0) .. (255, 255, 255) |
rgb01_to_rgbhex | RGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) | RGB string ‘#000000’ .. ‘#ffffff’ |
rgb255_to_hsl | RGB array (0, 0, 0) .. (255, 255, 255) | HSL array (0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0) |
rgb255_to_lab | RGB array (0, 0, 0) .. (255, 255, 255) | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) |
rgb255_to_lch | RGB array (0, 0, 0) .. (255, 255, 255) | D50 CIE L*C*h array (0.0, 0.0, 0.0) .. (100.0, *.*, 360.0) |
rgb255_to_rgb01 | RGB array (0, 0, 0) .. (255, 255, 255) | RGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) |
rgb255_to_rgbhex | RGB array (0, 0, 0) .. (255, 255, 255) | RGB string ‘#000000’ .. ‘#ffffff’ |
rgbhex_to_hsl | RGB string ‘#000000’ .. ‘#ffffff’ | HSL array (0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0) |
rgbhex_to_lab | RGB string ‘#000000’ .. ‘#ffffff’ | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) |
rgbhex_to_lch | RGB string ‘#000000’ .. ‘#ffffff’ | D50 CIE L*C*h array (0.0, 0.0, 0.0) .. (100.0, *.*, 360.0) |
rgbhex_to_rgb01 | RGB string ‘#000000’ .. ‘#ffffff’ | RGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) |
rgbhex_to_rgb255 | RGB string ‘#000000’ .. ‘#ffffff’ | RGB array (0, 0, 0) .. (255, 255, 255) |
srgb_to_lrgb | D65 non linear sRGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) | D65 linear sRGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) |
xyz_to_lab | D50 CIE XYZ array (*.*, *.*, *.*) .. (*.*, *.*, *.*) | D50 CIE L*a*b* array (0.0, *.*, *.*) .. (100.0, *.*, *.*) |
xyz_to_lrgb | D50 CIE XYZ array (*.*, *.*, *.*) .. (*.*, *.*, *.*) | D65 linear sRGB array (0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0) |
About
Color converter for Python 3.x. Supported formats: RGB, HSL, XYZ, L*a*b*, L*C*h.
mathebox / color_conversion.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
import math |
def rgb_to_hsv ( r , g , b ): |
r = float ( r ) |
g = float ( g ) |
b = float ( b ) |
high = max ( r , g , b ) |
low = min ( r , g , b ) |
h , s , v = high , high , high |
d = high — low |
s = 0 if high == 0 else d / high |
if high == low : |
h = 0.0 |
else : |
h = |
r : ( g — b ) / d + ( 6 if g < b else 0 ), |
g : ( b — r ) / d + 2 , |
b : ( r — g ) / d + 4 , |
>[ high ] |
h /= 6 |
return h , s , v |
def hsv_to_rgb ( h , s , v ): |
i = math . floor ( h * 6 ) |
f = h * 6 — i |
p = v * ( 1 — s ) |
q = v * ( 1 — f * s ) |
t = v * ( 1 — ( 1 — f ) * s ) |
r , g , b = [ |
( v , t , p ), |
( q , v , p ), |
( p , v , t ), |
( p , q , v ), |
( t , p , v ), |
( v , p , q ), |
][ int ( i % 6 )] |
return r , g , b |
def rgb_to_hsl ( r , g , b ): |
r = float ( r ) |
g = float ( g ) |
b = float ( b ) |
high = max ( r , g , b ) |
low = min ( r , g , b ) |
h , s , v = (( high + low ) / 2 ,) * 3 |
if high == low : |
h = 0.0 |
s = 0.0 |
else : |
d = high — low |
s = d / ( 2 — high — low ) if l > 0.5 else d / ( high + low ) |
h = |
r : ( g — b ) / d + ( 6 if g < b else 0 ), |
g : ( b — r ) / d + 2 , |
b : ( r — g ) / d + 4 , |
>[ high ] |
h /= 6 |
return h , s , v |
def hsl_to_rgb ( h , s , l ): |
def hue_to_rgb ( p , q , t ): |
t += 1 if t < 0 else 0 |
t -= 1 if t > 1 else 0 |
if t < 1 / 6 : return p + ( q - p ) * 6 * t |
if t < 1 / 2 : return q |
if t < 2 / 3 : p + ( q - p ) * ( 2 / 3 - t ) * 6 |
return p |
if s == 0 : |
r , g , b = l , l , l |
else : |
q = l * ( 1 + s ) if l < 0.5 else l + s - l * s |
p = 2 * l — q |
r = hue_to_rgb ( p , q , h + 1 / 3 ) |
g = hue_to_rgb ( p , q , h ) |
b = hue_to_rgb ( p , q , h — 1 / 3 ) |
return r , g , b |
def hsv_to_hsl ( h , s , v ): |
l = 0.5 * v * ( 2 — s ) |
s = v * s / ( 1 — math . fabs ( 2 * l — 1 )) |
return h , s , l |
def hsl_to_hsv ( h , s , l ): |
v = ( 2 * l + s * ( 1 — math . fabs ( 2 * l — 1 ))) / 2 |
s = 2 * ( v — l ) / v |
return h , s , v |