Generating color palette for Xmgrace

From LPTMS Wiki
Revision as of 13:42, 23 November 2011 by Roux (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

We provide some simple scripts that generate colormap for Xmgrace. Just copy paste the output in your grace file. Results can be viewed here.

grayscale colormap

<source lang="py">

  1. !/usr/bin/python

print "@map color 0 to (255, 255, 255), \"white\"" print "@map color 1 to (0, 0, 0), \"black\"" def LinearGray(num):

   N=int(255/num)
   for i in range(1,num):
       n = str(N*i)
       print "@map color "+str(i+1)+" to ("+n+','+n+','+n+"), \"gray"+str(i)+"\""

LinearGray(12) </source>

HSV colormap and Gradients

Use RGB to HSV and reciprocal conversion. Gradients are much easier to derive in HSV format. One can use 'sin' functions instead of linear ones.

<source lang="py">

  1. !/usr/bin/python

print "@map color 0 to (255, 255, 255), \"white\"" print "@map color 1 to (0, 0, 0), \"black\""

def RGBtoHSV( (R,G,B) , fac = 255.0 ):

   R /= fac
   G /= fac
   B /= fac
   M = max(R,G,B)
   m = min(R,G,B)
   C = M-m
   Hp = 0
   if not C==0:

if M==R: Hp = ((G-B)/C)%6 elif M==G: Hp = (B-R)/C + 2 elif M==B: Hp = (R-G)/C + 4

   H = Hp*60
   V = M
   S = C/V
   return (H,S,V)

def HSVtoRGB( (H,S,V) ):

   C = V*S
   Hp = H/60
   X = C*(1-abs(Hp%2-1))
   (R1,G1,B1) = (0,0,0)
   if   0<=Hp and Hp<1: (R1,G1,B1) = (C,X,0)
   elif 1<=Hp and Hp<2: (R1,G1,B1) = (X,C,0)
   elif 2<=Hp and Hp<3: (R1,G1,B1) = (0,C,X)
   elif 3<=Hp and Hp<4: (R1,G1,B1) = (0,X,C)
   elif 4<=Hp and Hp<5: (R1,G1,B1) = (X,0,C)
   elif 5<=Hp and Hp<6: (R1,G1,B1) = (C,0,X)
   m = V-C
   (R,G,B) = (R1+m,G1+m,B1+m)
   return (int(255*R),int(255*G),int(255*B))

def HSVGradient(start,stop,num):

   N=int(256/(num-1))
   (h1,s1,v1) = start
   (h2,s2,v2) = stop
   (hs,ss,vs) = ((h2-h1)/float(num-1),(s2-s1)/float(num-1),(v2-v1)/float(num-1))
   for i in range(num):

(h,s,v) = (h1+i*hs,s1+i*ss,v1+i*vs) (r,g,b) = HSVtoRGB( (h,s,v) ) print "@map color "+str(i+2)+" to "+str((r,g,b))+", \"grad"+str(i+1)+"\""

rgbred, rgbgreen, rgbblue = (255,0,0), (0,255,0), (0,0,255) hsvred, hsvgreen, hsvblue = RGBtoHSV(rgbred), RGBtoHSV(rgbgreen), RGBtoHSV(rgbblue)

HSVGradient(hsvred,hsvblue,11)

  1. HSVGradient((0.0,1.0,1.0),(240.0,1.0,1.0),24)

</source>

Lighter colors

useful if you want to fill object and curves with a lighter color:

<source lang="py">

  1. !/usr/bin/python

black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) indigo = (114, 33, 188) magenta = (255, 0, 255) orange = (255, 165, 0) green4 = (0, 139, 0) maroon = (103, 7, 72) colors = [ red,blue,green,indigo,magenta,orange,green4,maroon ] name = { red:'red', blue:'blue', green:'green', indigo:'indigo', magenta:'magenta', orange:'orange', green4:'green4', maroon:'maroon' }

vfac=2 sfac=0.08 grey=int(255*(1-sfac))

i=2 print "@map color 0 to (255, 255, 255), \"white\"" print "@map color 1 to (0, 0, 0), \"black\"" for c in colors:

   print "@map color "+str(i)+" to "+str(c)+", \""+name[c]+"\""
   i+=1

print "@map color "+str(i)+" to "+str( (grey,grey,grey) )+", \"grey\"" i+=1 for c in colors:

   hsv = RGBtoHSV(c)
   lighter = (hsv[0],sfac*hsv[1],min(1.0,vfac*hsv[2]))
   print "@map color "+str(i)+" to "+str(HSVtoRGB(lighter))+", \"light"+name[c]+"\""
   i+=1

</source>