Import color palettes

From Processing

Jump to: navigation, search
Versions: 1.0+
Contributors: eskimoblood
Started: 2006-09-20


There are some sites, like Color Schemer, ColorBlender, ColorMatch Redux, that offer color palettes in the .act and .cs file format. This little hack will import these files and store the colors in an array.

Source Code

/**
Import color palettes taken from http://wiki.processing.org/index.php?title=Import_color_palettes
@author Andreas Köberle
*/
 
class Palette{
  color[] colors;
  Palette(String file){
    byte[] b=loadBytes(file);
    if(file.endsWith(".cs")){
      createPalette(b,8,26,b[2]&0xff);
    }
    else if (file.endsWith(".act")){
      createPalette(b,0,3,255);
    }
  }
  Palette(String file, int length){
    byte[] b=loadBytes(file);
    if(file.endsWith(".cs")){
      createPalette(b,8,26,length);
    }
    else if (file.endsWith(".act")){
      createPalette(b,0,3,length);
    }
  }
  void createPalette(byte[] b, int start, int steps, int length){
    colors=new color[length];
    int cnt=0;
    for(int i=0 ;i<length;i++){
      colors[i]=0xff<<24|b[start+i*steps]<<16|b[start+i*steps+1]<<8|b[start+i*steps+2];
    }
  }
}
 
void setup(){
  Palette p=new Palette("test.cs");
  color cl=p.colors[0];
  background(cl);
}

Downloads

Related Links

Personal tools