Skip to content

Instantly share code, notes, and snippets.

@lucianmarin
Created April 13, 2017 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucianmarin/7352579975b596ebd3e1fa6dfa87a184 to your computer and use it in GitHub Desktop.
Save lucianmarin/7352579975b596ebd3e1fa6dfa87a184 to your computer and use it in GitHub Desktop.
HSP color space was created Darel Rex Finley http://alienryderflex.com/hsp.html
#!/usr/bin/python
import math
import sys
print sys.argv[1]
print sys.argv[2]
print sys.argv[3]
R = float(sys.argv[1])
G = float(sys.argv[2])
B = float(sys.argv[3])
Pr = 0.299
Pg = 0.587
Pb = 0.114
R = (R+1)/256
G = (G+1)/256
B = (B+1)/256
P = math.sqrt(R*R*Pr + G*G*Pg + B*B*Pb)
if R == G and R == B:
H = 0
S = 0
else:
if R >= G and R >= B: # R is largest
if B >= G:
H = 6./6. - 1./6.*(B-G)/(R-G)
S = 1. - G/R
else:
H = 0./6. + 1./6.*(G-B)/(R-B)
S = 1. - B/R
else:
if G >= R and G >= B: # G is largest
if R >= B:
H = 2./6. - 1./6.*(R-B)/(G-B)
S = 1. - B/G
else:
H = 2./6. + 1./6.*(B-R)/(G-R)
S = 1. - R/G
else: # B is largest
if G >= R:
H = 4./6. - 1./6.*(G-R)/(B-R)
S = 1. - R/B
else:
H = 4./6. + 1./6.*(R-G)/(B-G)
S = 1. - G/B
print 'h: ' + str(H*360)
print 's: ' + str(S*100)
print 'p: ' + str(P*100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment