Can't address correctly an array..
in
Programming Questions
•
1 years ago
I load a .cs file using a snippet found on Processing-wiki. It works perfectly, but when I try to use it, with minor alteration, I get nullerror exception. The cs file is in the directory and it work as long as I address it within the setup ?!?
// ColorPalette
/**
Import color palettes taken from
http://wiki.processing.org/index.php?title=Import_color_palettes
@author Andreas Köberle
*/
Palette p;// this is new
color cl;// new too
void setup(){
Palette p=new Palette("color1.cs");
size(400,400);
color cl=p.colors[0];
background(cl);
}
// problem in draw
void draw(){
for (int i=0;i<p.colors.length;i++){
cl=p.colors[i];
fill(cl);
rect(20*i,20,10,20);
}
}
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];
}
}
}
1