###### *** RGB-to-Gray Palette Driver *** ###### # PARAMETERS (to be set before calling this script) # colormode : 0 - convert to grayscale # 1 - keep RGB color # -1 - old (obsolete) gray-RGB saturation # else - gray-RGB saturation # 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) # negativemode : 0 - all positive # 1 - palette negative, lines/text positive # 2 - all negative # 3 - palette positive, lines/text negative # rgbfnmode : 1 - use RGB formulae # else - use palette file # f1(x),f2(x),f3(x): RGB formulae; any function [0:1] -> [0:1] # try funcs from rgbformulae.gp # use fn(1-x) for "negative" and 1-fn(x) for inverse # permrgb : permutation of RGB colors; default=123 # dimr,g,b : dim R,G,B by a factor 0...1 # wlo,whi : lower, upper gray value (0=black, 1=white) # sat : color saturation 0--1 # pgamma : Gamma correction (gray) # pgamma_aux : Ausxiliary additional gamma for palette only # gamr, gamg, gamb : RGB gamma exponents # gammamode : 0 - gray only # 1 - gamrgb=pgamma # 2 - individual RGB gammas # 3 - like 2 but also scaled by pgamma # else - all = 1 # # EXAMPLE: # load 'rgbformulae.gp' # f1(x)=frgb07(x);f2(x)=frgb05(x);f3(x)=frgb15(x) # wr=0.3; wg=0.6; wb=0.1 # colormode=1;graymode=1;rgbfnmode=0;gammamode=0 # sat=1.;pgamma=1.;gamr=1.;gamg=1.;gamb=1. # permrgb=123;dimr=1.;dimg=1.;dimb=1. # load 'colormanager.gp' # ###### Check if variables are defined ##################################### if (!exists("colormode")) colormode=1 if (!exists("negativemode")) negativemode=0 if (!exists("rgbfnmode")) rgbfnmode=0 if (!exists("f1")) f1(x)=frgb07(x) if (!exists("f2")) f2(x)=frgb05(x) if (!exists("f3")) f3(x)=frgb15(x) if (!exists("permrgb")) permrgb=123 if (!exists("dimgray")) dimgray=1. if (!exists("dimr")) dimr=1. if (!exists("dimg")) dimg=1. if (!exists("dimb")) dimb=1. if (!exists("wr")) wr=0.3 if (!exists("wg")) wg=0.6 if (!exists("wb")) wb=0.1 if (!exists("wlo")) wlo=0. if (!exists("whi")) wlo=1. if (!exists("sat")) sat=1. if (!exists("pgamma")) pgamma=1. if (!exists("gamr")) gamr=1. if (!exists("gamg")) gamg=1. if (!exists("gamb")) gamb=1. if (!exists("gammamode")) gammamode=1 #if (!exists("")) ###### Input Area ######################################################### ### Define custom functions and gamma correction here (or outside) # Gamma mode gammamode10=int(gammamode/10) gammamode01=gammamode-10*gammamode10 print 'gammamode = ',gammamode print 'gammamode01 = ',gammamode01 print 'gammamode10 = ',gammamode10 if (gammamode10<=0) pgamma_aux=1. if (gammamode01==0) gamr=1. ; gamg=1. ; gamb=1. ;\ else if (gammamode01==1) gamr=pgamma; gamg=gamr ; gamb=gamr ;\ else if (gammamode01==3) gamr=gamr*pgamma;gamg=gamg*pgamma;gamb=gamb*pgamma; # Trap bad gamma and weights inputs if (pgamma<=0) pgamma = 1. # no gamma correction at all if (gamr<=0 || gamg<=0 || gamb<=0) gamr=1. ; gamg=1. ; gamb=1. if (wr<=0 || wg<=0 || wb<=0) wr=1.; wg=1.; wb=1. ### Pre-defined 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) 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;\ #(else: custom weights defined in before calling) ### some definitions & rescalings ww=wr+wg+wb; wr=wr/ww;wg=wg/ww;wb=wb/ww#rescale user-defined weights print 'ww,wr,wg,wb = ',ww,wr,wg,wb #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 wmap(x)=wlo+(whi-wlo)*x tas=1.-sat qr=sat*gamr+tas*pgamma; qg=sat*gamg+tas*pgamma; qb=sat*gamb+tas*pgamma print "gamma = ",pgamma print "g_rgb = ",gamr,gamg,gamb print "q_rgb = ",qr,qg,qb,"; saturation = ",sat ### Define weighted RGB-to-gray function (don't touch!) wgray(r,g,b)=wr*r+wg*g+wb*b fgray(r,g,b)=wgray(r,g,b)**pgamma # ###### Color Selection Area ############################################### # 0: grayscale, 1: color, -1: old interpolation, # else: saturation interpolation if (colormode==0) \ fred(r,g,b)=fgray(r,g,b);\ fgreen(r,g,b)=fgray(r,g,b);\ fblue(r,g,b)=fgray(r,g,b);\ else if (colormode==1) \ fred(r,g,b)=r**gamr;fgreen(r,g,b)=g**gamg;fblue(r,g,b)=b**gamb;\ else if (colormode==-1) \ fred(r,g,b) =sat*r**gamr+tas*fgray(r,g,b);\ fgreen(r,g,b)=sat*g**gamg+tas*fgray(r,g,b);\ fblue(r,g,b) =sat*b**gamb+tas*fgray(r,g,b);\ else \ fred(r,g,b) =(sat*r+tas*wgray(r,g,b))**qr;\ fgreen(r,g,b)=(sat*g+tas*wgray(r,g,b))**qg;\ fblue(r,g,b) =(sat*b+tas*wgray(r,g,b))**qb # Color inversion if (negativemode==1 || negativemode==2) sgnpalette=-1.; else sgnpalette=1. if (negativemode==2 || negativemode==3) sgnlinetxt=-1.; else sgnlinetxt=1. dimtr=dimr*sgnlinetxt; dimtg=dimg*sgnlinetxt; dimtb=dimb*sgnlinetxt dimpr=dimr*sgnpalette; dimpg=dimg*sgnpalette; dimpb=dimb*sgnpalette if (dimpr<0.) offr=-dimpr; else offr=0. if (dimpg<0.) offg=-dimpg; else offg=0. if (dimpb<0.) offb=-dimpb; else offb=0. if (dimtr<0.) offtr=-dimtr; else offtr=0. if (dimtg<0.) offtg=-dimtg; else offtg=0. if (dimtb<0.) offtb=-dimtb; else offtb=0. if (gammamode10>0) \ funcr(r,g,b)=wmap(offr+dimpr*fred(r,g,b))**pgamma_aux;\ funcg(r,g,b)=wmap(offg+dimpg*fgreen(r,g,b))**pgamma_aux;\ funcb(r,g,b)=wmap(offb+dimpb*fblue(r,g,b))**pgamma_aux;\ else \ funcr(r,g,b)=wmap(offr+dimpr*fred(r,g,b));\ funcg(r,g,b)=wmap(offg+dimpg*fgreen(r,g,b));\ funcb(r,g,b)=wmap(offb+dimpb*fblue(r,g,b)) functr(r,g,b)=wmap(offtr+dimtr*fred(r,g,b)) functg(r,g,b)=wmap(offtg+dimtg*fgreen(r,g,b)) functb(r,g,b)=wmap(offtb+dimtb*fblue(r,g,b)) ### for rgbstring ######################################################### fr255(r,g,b)=int(255.*functr(r/255.,g/255.,b/255.)) fg255(r,g,b)=int(255.*functg(r/255.,g/255.,b/255.)) fb255(r,g,b)=int(255.*functb(r/255.,g/255.,b/255.)) rgb255(r,g,b)=sprintf("#%06x",65536*r+256*g+b) #0 to 255 f2rgb255(r,g,b)=rgb255(fr255(r,g,b),fg255(r,g,b),fb255(r,g,b)) ########################################################################### # ###### Palette ############################################################ ### Set the palette via functions or file perm1=int(permrgb/100.); dummy=permrgb-100.*perm1 perm2=int(dummy/10.); dummy=dummy-10.*perm2 perm3=int(dummy) if(perm1<1) perm1=1 if(perm2<1) perm2=2 if(perm3<1) perm3=3 if(perm1>3) perm1=perm1-3 if(perm2>3) perm2=perm2-3 if(perm3>3) perm3=perm3-3 c1=perm1+1; c2=perm2+1; c3=perm3+1 print "negativemode = ",negativemode print "sgnpalette = ",sgnpalette print "sgnlinetxt = ",sgnlinetxt print "pgamma, aux., eff.= ",pgamma,pgamma_aux,pgamma*pgamma_aux print "dimr,dimg,dimb = ",dimr,dimg,dimb print "dimpr,dimpg,dimpb = ",dimpr,dimpg,dimpb print "dimtr,dimtg,dimtb = ",dimtr,dimtg,dimtb print "offr,offg,offb = ",offr,offg,offb print "offtr,offtg,offtb = ",offtr,offtg,offtb print "perm1,perm2,perm3 = ",perm1,perm2,perm3 print "c1,c2,c3 = ",c1,c2,c3 if (rgbfnmode==1) \ print "Using RGB functions"; \ set palette function funcr(f1(gray),f2(gray),f3(gray)),\ funcg(f1(gray),f2(gray),f3(gray)),\ funcb(f1(gray),f2(gray),f3(gray));\ else \ print "Using palette file '",palettefile,"'" ; \ set palette file palettefile using \ 1:(funcr(column(c1),column(c2),column(c3)))\ :(funcg(column(c1),column(c2),column(c3)))\ :(funcb(column(c1),column(c2),column(c3))) ### grayscale, gamma-weighted: #set palette function gray**gamr,gray**gamg,gray**gamb