###### RGB definitions #################################################### # Define the following parameters before calling this: # pgamma : gamma correction # sat : color saturation # wlo,whi : lower, upper gray value (0=black, 1=white) # wr,wg,wb : user-defined gray weighting (for graymode !=0,1,2,3) # graymode : gray weighting mode # 0 - simple equal weighting # 1 - CCIR 601 (used e.g. by the GIMP) # 2 - SMPTE 240M (1035i HDTV) # 3 - ITU-R (modern HDTV) # else - custom wr,wg,wb (defined before calling) ## Example (full color, no gamma, no remapping): ## sat=1.; pgamma=1.; wlo=0.; whi=1.; graymode=1 ## wr=0.24; wg=0.64; wb=0.12 #### if (sat>1) sat=1 if (sat<0) sat=0 if (wlo>1) wlo=1 if (wlo<0) wlo=0 if (whi>1) whi=1 if (whi<0) whi=0 tas=1.-sat wmap(x)=wlo+(whi-wlo)*x ### Pre-defined gray weighting mode #wr=1./3.; wg=wr; wb=wr # 0: simple equal weighting #wr=0.299; wg=0.587; wb=0.114 # 1: CCIR 601 (used e.g. by the GIMP) #wr=0.212; wg=0.701; wb=0.087 # 2: SMPTE 240M (1035i HDTV) #wr=0.2126;wg=0.7152;wb=0.0722 # 3: ITU-R (modern HDTV) if (graymode==0) wr=1./3.; wg=wr; wb=wr;\ else if (graymode==1) wr=0.299; wg=0.587; wb=0.114;\ else if (graymode==2) wr=0.212; wg=0.701; wb=0.087;\ else if (graymode==3) wr=0.2126;wg=0.7152;wb=0.0722 ww=wr+wg+wb; wr=wr/ww;wg=wg/ww;wb=wb/ww#rescale user-defined weights #(else: custom weights defined in before calling) wgray(r,g,b)=wr*r+wg*g+wb*b fr(r,g,b)=int(255.*wmap(((sat*r+tas*wgray(r,g,b))/255.)**pgamma)) fg(r,g,b)=int(255.*wmap(((sat*g+tas*wgray(r,g,b))/255.)**pgamma)) fb(r,g,b)=int(255.*wmap(((sat*b+tas*wgray(r,g,b))/255.)**pgamma)) #### String conversion rgb255(r,g,b)=sprintf("#%06x",65536*r+256*g+b) #0 to 255 f2rgb255(r,g,b)=rgb255(fr(r,g,b),fg(r,g,b),fb(r,g,b))