"""Conversion functions between RGB and other color systems.This modules provides two functions for each color system ABC: rgb_to_abc(r, g, b) --> a, b, c abc_to_rgb(a, b, c) --> r, g, bAll inputs and outputs are triples of floats in the range [0.0...1.0](with the exception of I and Q, which covers a slightly larger range).Inputs outside the valid range may cause exceptions or invalid outputs.Supported color systems:RGB: Red, Green, Blue componentsYIQ: Luminance, Chrominance (used by composite video signals)HLS: Hue, Luminance, SaturationHSV: Hue, Saturation, Value"""# References:# http://en.wikipedia.org/wiki/YIQ# http://en.wikipedia.org/wiki/HLS_color_space# http://en.wikipedia.org/wiki/HSV_color_space__all__=["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb","rgb_to_hsv","hsv_to_rgb"]# Some floating point constantsONE_THIRD=1.0/3.0ONE_SIXTH=1.0/6.0TWO_THIRD=2.0/3.0# YIQ: used by composite video signals (linear combinations of RGB)# Y: perceived grey level (0.0 == black, 1.0 == white)# I, Q: color components## There are a great many versions of the constants used in these formulae.# The ones in this library uses constants from the FCC version of NTSC.defrgb_to_yiq(r,g,b):y=0.30*r+0.59*g+0.11*bi=0.74*(r-y)-0.27*(b-y)q=0.48*(r-y)+0.41*(b-y)return(y,i,q)defyiq_to_rgb(y,i,q):# r = y + (0.27*q + 0.41*i) / (0.74*0.41 + 0.27*0.48)# b = y + (0.74*q - 0.48*i) / (0.74*0.41 + 0.27*0.48)# g = y - (0.30*(r-y) + 0.11*(b-y)) / 0.59r=y+0.9468822170900693*i+0.6235565819861433*qg=y-0.27478764629897834*i-0.6356910791873801*qb=y-1.1085450346420322*i+1.7090069284064666*qifr<0.0:r=0.0ifg<0.0:g=0.0ifb<0.0:b=0.0ifr>1.0:r=1.0ifg>1.0:g=1.0ifb>1.0:b=1.0return(r,g,b)# HLS: Hue, Luminance, Saturation# H: position in the spectrum# L: color lightness# S: color saturationdefrgb_to_hls(r,g,b):maxc=max(r,g,b)minc=min(r,g,b)# XXX Can optimize (maxc+minc) and (maxc-minc)l=(minc+maxc)/2.0ifminc==maxc:return0.0,l,0.0ifl<=0.5:s=(maxc-minc)/(maxc+minc)else:s=(maxc-minc)/(2.0-maxc-minc)rc=(maxc-r)/(maxc-minc)gc=(maxc-g)/(maxc-minc)bc=(maxc-b)/(maxc-minc)ifr==maxc:h=bc-gcelifg==maxc:h=2.0+rc-bcelse:h=4.0+gc-rch=(h/6.0)%1.0returnh,l,sdefhls_to_rgb(h,l,s):ifs==0.0:returnl,l,lifl<=0.5:m2=l*(1.0+s)else:m2=l+s-(l*s)m1=2.0*l-m2return(_v(m1,m2,h+ONE_THIRD),_v(m1,m2,h),_v(m1,m2,h-ONE_THIRD))def_v(m1,m2,hue):hue=hue%1.0ifhue<ONE_SIXTH:returnm1+(m2-m1)*hue*6.0ifhue<0.5:returnm2ifhue<TWO_THIRD:returnm1+(m2-m1)*(TWO_THIRD-hue)*6.0returnm1# HSV: Hue, Saturation, Value# H: position in the spectrum# S: color saturation ("purity")# V: color brightnessdefrgb_to_hsv(r,g,b):maxc=max(r,g,b)minc=min(r,g,b)v=maxcifminc==maxc:return0.0,0.0,vs=(maxc-minc)/maxcrc=(maxc-r)/(maxc-minc)gc=(maxc-g)/(maxc-minc)bc=(maxc-b)/(maxc-minc)ifr==maxc:h=bc-gcelifg==maxc:h=2.0+rc-bcelse:h=4.0+gc-rch=(h/6.0)%1.0returnh,s,vdefhsv_to_rgb(h,s,v):ifs==0.0:returnv,v,vi=int(h*6.0)# XXX assume int() truncates!f=(h*6.0)-ip=v*(1.0-s)q=v*(1.0-s*f)t=v*(1.0-s*(1.0-f))i=i%6ifi==0:returnv,t,pifi==1:returnq,v,pifi==2:returnp,v,tifi==3:returnp,q,vifi==4:returnt,p,vifi==5:returnv,p,q# Cannot get here