Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f72bbb

Browse files
committed
Jack added new color systems (unfortunately using old lay-out).
1 parent 3e0d556 commit 8f72bbb

1 file changed

Lines changed: 100 additions & 24 deletions

File tree

Demo/sgi/video/colorsys.py

Lines changed: 100 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,106 @@
1-
# Conversions between RGB and YIQ color spaces.
21
#
3-
# R, G, B are amounts of Red, Green and Blue;
4-
# Y, I, Q are intensity (Y) and two difference signals (I, Q).
5-
2+
# Module color - do color conversions
3+
#
64

7-
# Convert a (R, G, B) triple to (Y, I, Q).
8-
# Scale is arbitrary (output uses the same scale as input).
5+
ONE_THIRD=1.0/3.0
6+
ONE_SIXTH=1.0/6.0
7+
TWO_THIRD=2.0/3.0
98

10-
def rgb_to_yiq(r, g, b):
11-
y = 0.30*r + 0.59*g + 0.11*b
12-
i = 0.60*r - 0.28*g - 0.32*b
13-
q = 0.21*r - 0.52*g + 0.31*b
14-
return y, i, q
9+
def rgb_to_yiq(r,g,b):
10+
y = 0.3*r + 0.59*g + 0.11*b
11+
i = 0.6*r - 0.28*g - 0.32*b
12+
q = 0.21*r- 0.52*g + 0.31*b
13+
return (y,i,q)
14+
def yiq_to_rgb(y,i,q):
15+
r = y + 0.948262*i + 0.624013*q
16+
g = y - 0.276066*i - 0.639810*q
17+
b = y - 1.105450*i + 1.729860*q
18+
if r < 0.0: r = 0.0
19+
if g < 0.0: g = 0.0
20+
if b < 0.0: b = 0.0
21+
if r > 1.0: r = 1.0
22+
if g > 1.0: g = 1.0
23+
if b > 1.0: b = 1.0
24+
return (r,g,b)
1525

26+
def _v(m1,m2,hue):
27+
if hue >= 1.0: hue = hue - 1.0
28+
if hue < 0.0: hue = hue + 1.0
29+
if hue < ONE_SIXTH:
30+
return m1 + (m2-m1)*hue*6.0
31+
if hue < 0.5:
32+
return m2
33+
if hue < TWO_THIRD:
34+
return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
35+
return m1
1636

17-
# Convert a (Y, I, Q) triple to (R, G, B).
18-
# Input and output values are in the interval [0.0 ... 1.0].
37+
def rgb_to_hls(r,g,b):
38+
maxc = max(r,g,b)
39+
minc = min(r,g,b)
40+
l = (minc+maxc)/2.0
41+
if minc == maxc:
42+
return 0.0, l, 0.0
43+
if l <= 0.5:
44+
s = (maxc-minc)/(maxc+minc)
45+
else:
46+
s = (maxc-minc)/(2-maxc-minc)
47+
rc = (maxc-r)/(maxc-minc)
48+
gc = (maxc-g)/(maxc-minc)
49+
bc = (maxc-b)/(maxc-minc)
50+
if r == maxc:
51+
h = bc-gc
52+
elif g == maxc:
53+
h = 2.0+rc-bc
54+
else:
55+
h = 4.0+gc-rc
56+
h = h/6.0
57+
if h < 0.0:
58+
h = h + 1.0
59+
return h,l,s
60+
def hls_to_rgb(h,l,s):
61+
if s == 0.0:
62+
return l,l,l
63+
if l <= 0.5:
64+
m2 = l * (1.0+s)
65+
else:
66+
m2 = l+s-(l*s)
67+
m1 = 2.0*l - m2
68+
return (_v(m1,m2,h+ONE_THIRD), _v(m1,m2,h), _v(m1,m2,h-ONE_THIRD))
1969

20-
def yiq_to_rgb(y, i, q):
21-
r = y + 0.948262*i + 0.624013*q
22-
g = y - 0.276066*i - 0.639810*q
23-
b = y - 1.105450*i + 1.729860*q
24-
if r < 0.0: r = 0.0
25-
if g < 0.0: g = 0.0
26-
if b < 0.0: b = 0.0
27-
if r > 1.0: r = 1.0
28-
if g > 1.0: g = 1.0
29-
if b > 1.0: b = 1.0
30-
return r, g, b
70+
def rgb_to_hsv(r,g,b):
71+
maxc = max(r,g,b)
72+
minc = min(r,g,b)
73+
v = maxc
74+
if minc == maxc:
75+
return 0.0, 0.0, v
76+
s = (maxc-minc)/maxc
77+
rc = (maxc-r)/(maxc-minc)
78+
gc = (maxc-g)/(maxc-minc)
79+
bc = (maxc-b)/(maxc-minc)
80+
if r == maxc:
81+
h = bc-gc
82+
elif g == maxc:
83+
h = 2.0+rc-bc
84+
else:
85+
h = 4.0+gc-rc
86+
h = h/6.0
87+
if h < 0.0:
88+
h = h + 1.0
89+
return h,s,v
90+
def hsv_to_rgb(h,s,v):
91+
if s == 0.0:
92+
return v,v,v
93+
i = int(h*6.0)
94+
f = (h*6.0)-i
95+
p = v*(1.0-s)
96+
q = v*(1.0-s*f)
97+
t = v*(1.0-s*(1.0-f))
98+
if i in (0,6): return v,t,p
99+
if i = 1: return q,v,p
100+
if i = 2: return p,v,t
101+
if i = 3: return p,q,v
102+
if i = 4: return t,p,v
103+
if i = 5: return v,p,q
104+
print i, h, f
105+
print h, s, v
106+
raise 'Bad color'

0 commit comments

Comments
 (0)