]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/colorsys.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / colorsys.py
CommitLineData
4710c53d 1"""Conversion functions between RGB and other color systems.\r
2\r
3This modules provides two functions for each color system ABC:\r
4\r
5 rgb_to_abc(r, g, b) --> a, b, c\r
6 abc_to_rgb(a, b, c) --> r, g, b\r
7\r
8All inputs and outputs are triples of floats in the range [0.0...1.0]\r
9(with the exception of I and Q, which covers a slightly larger range).\r
10Inputs outside the valid range may cause exceptions or invalid outputs.\r
11\r
12Supported color systems:\r
13RGB: Red, Green, Blue components\r
14YIQ: Luminance, Chrominance (used by composite video signals)\r
15HLS: Hue, Luminance, Saturation\r
16HSV: Hue, Saturation, Value\r
17"""\r
18\r
19# References:\r
20# http://en.wikipedia.org/wiki/YIQ\r
21# http://en.wikipedia.org/wiki/HLS_color_space\r
22# http://en.wikipedia.org/wiki/HSV_color_space\r
23\r
24__all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",\r
25 "rgb_to_hsv","hsv_to_rgb"]\r
26\r
27# Some floating point constants\r
28\r
29ONE_THIRD = 1.0/3.0\r
30ONE_SIXTH = 1.0/6.0\r
31TWO_THIRD = 2.0/3.0\r
32\r
33# YIQ: used by composite video signals (linear combinations of RGB)\r
34# Y: perceived grey level (0.0 == black, 1.0 == white)\r
35# I, Q: color components\r
36\r
37def rgb_to_yiq(r, g, b):\r
38 y = 0.30*r + 0.59*g + 0.11*b\r
39 i = 0.60*r - 0.28*g - 0.32*b\r
40 q = 0.21*r - 0.52*g + 0.31*b\r
41 return (y, i, q)\r
42\r
43def yiq_to_rgb(y, i, q):\r
44 r = y + 0.948262*i + 0.624013*q\r
45 g = y - 0.276066*i - 0.639810*q\r
46 b = y - 1.105450*i + 1.729860*q\r
47 if r < 0.0:\r
48 r = 0.0\r
49 if g < 0.0:\r
50 g = 0.0\r
51 if b < 0.0:\r
52 b = 0.0\r
53 if r > 1.0:\r
54 r = 1.0\r
55 if g > 1.0:\r
56 g = 1.0\r
57 if b > 1.0:\r
58 b = 1.0\r
59 return (r, g, b)\r
60\r
61\r
62# HLS: Hue, Luminance, Saturation\r
63# H: position in the spectrum\r
64# L: color lightness\r
65# S: color saturation\r
66\r
67def rgb_to_hls(r, g, b):\r
68 maxc = max(r, g, b)\r
69 minc = min(r, g, b)\r
70 # XXX Can optimize (maxc+minc) and (maxc-minc)\r
71 l = (minc+maxc)/2.0\r
72 if minc == maxc:\r
73 return 0.0, l, 0.0\r
74 if l <= 0.5:\r
75 s = (maxc-minc) / (maxc+minc)\r
76 else:\r
77 s = (maxc-minc) / (2.0-maxc-minc)\r
78 rc = (maxc-r) / (maxc-minc)\r
79 gc = (maxc-g) / (maxc-minc)\r
80 bc = (maxc-b) / (maxc-minc)\r
81 if r == maxc:\r
82 h = bc-gc\r
83 elif g == maxc:\r
84 h = 2.0+rc-bc\r
85 else:\r
86 h = 4.0+gc-rc\r
87 h = (h/6.0) % 1.0\r
88 return h, l, s\r
89\r
90def hls_to_rgb(h, l, s):\r
91 if s == 0.0:\r
92 return l, l, l\r
93 if l <= 0.5:\r
94 m2 = l * (1.0+s)\r
95 else:\r
96 m2 = l+s-(l*s)\r
97 m1 = 2.0*l - m2\r
98 return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))\r
99\r
100def _v(m1, m2, hue):\r
101 hue = hue % 1.0\r
102 if hue < ONE_SIXTH:\r
103 return m1 + (m2-m1)*hue*6.0\r
104 if hue < 0.5:\r
105 return m2\r
106 if hue < TWO_THIRD:\r
107 return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0\r
108 return m1\r
109\r
110\r
111# HSV: Hue, Saturation, Value\r
112# H: position in the spectrum\r
113# S: color saturation ("purity")\r
114# V: color brightness\r
115\r
116def rgb_to_hsv(r, g, b):\r
117 maxc = max(r, g, b)\r
118 minc = min(r, g, b)\r
119 v = maxc\r
120 if minc == maxc:\r
121 return 0.0, 0.0, v\r
122 s = (maxc-minc) / maxc\r
123 rc = (maxc-r) / (maxc-minc)\r
124 gc = (maxc-g) / (maxc-minc)\r
125 bc = (maxc-b) / (maxc-minc)\r
126 if r == maxc:\r
127 h = bc-gc\r
128 elif g == maxc:\r
129 h = 2.0+rc-bc\r
130 else:\r
131 h = 4.0+gc-rc\r
132 h = (h/6.0) % 1.0\r
133 return h, s, v\r
134\r
135def hsv_to_rgb(h, s, v):\r
136 if s == 0.0:\r
137 return v, v, v\r
138 i = int(h*6.0) # XXX assume int() truncates!\r
139 f = (h*6.0) - i\r
140 p = v*(1.0 - s)\r
141 q = v*(1.0 - s*f)\r
142 t = v*(1.0 - s*(1.0-f))\r
143 i = i%6\r
144 if i == 0:\r
145 return v, t, p\r
146 if i == 1:\r
147 return q, v, p\r
148 if i == 2:\r
149 return p, v, t\r
150 if i == 3:\r
151 return p, q, v\r
152 if i == 4:\r
153 return t, p, v\r
154 if i == 5:\r
155 return v, p, q\r
156 # Cannot get here\r