Color Palette: Two arrays
in
Programming Questions
•
2 years ago
Hey, I've finally decided to take a crack at arrays and decided to create a simple color palette for a little paint program I'm making. I've seen tons of different examples of how someone would go about doing and I have a question. I'm trying to build my color palette with two arrays. One 2D array to make columns and rows and one 1D array to store the colors. I got it to show the rectangles in a 2 by 10 table but can't seem to be able to get the fill of each rectangle to be different. I actually succeeded (partially) but changed it to get it working completely, failed, and forgot how I partially succeeded... So now I ask for help after looking at array tutorials and tons of examples of arrays and using a combination of arrays to make colors and multiple shapes but can't seem to figure it out. I'd really appreciate it if someone could tell me if this is possible or if I'm doing something wrong. Here's the code I have so far and what I've been trying to use to make my palette work.
My Code
Oh and a little off topic, I have another post about controlP5 and have found nothing related to what I'm asking in that post and have had no replies... I started the topic in february... so I'd really appreciate the help. Here's the link: ControlP5 Post
Thanks for all the help,
TheGamersHaven
My Code
- int cols=2;
- int rows=10;
- color f1 = color(0);
- color f2 = color(255);
- Cell[][] palette = new Cell[cols][rows];
- color[] rectCol = new color[20];
- void setup() {
- size(500,500);
- smooth();
- background(255);
- for (int i=0; i<cols; i++) {
- for (int j=0; j<rows; j++) {
- palette[i][j] = new Cell(i*20,j*20,20,20,i);
- }
- }
- rectCol[0] = color(0); //black
- rectCol[1] = color(255); //white
- rectCol[2] = color(46, 11, 11); //brown
- rectCol[3] = color(120, 10, 10); // dark red
- rectCol[4] = color(255, 0, 0); //red
- rectCol[5] = color(250, 71, 74); // light red
- rectCol[6] = color(245, 59, 255); //pinkish
- rectCol[7] = color(232, 0, 245); // pink
- rectCol[8] = color(129, 3, 137); //purple
- rectCol[9] = color(55, 55, 250); //light blue
- rectCol[10] = color(0, 0, 255); //blue
- rectCol[11] = color(0, 0, 150); //dark blue
- rectCol[12] = color(95, 247, 101); //light green
- rectCol[13] = color(0, 247, 10); //lime green
- rectCol[14] = color(0, 103, 4); //dark green
- rectCol[15] = color(58, 103, 0); //yellow green
- rectCol[16] = color(255, 255, 0); //yellow
- rectCol[17] = color(255, 100, 0); //orange
- rectCol[18] = color(155); // gray
- rectCol[19] = color(55); //grayer
- }
- void draw() {
- for (int i=0; i<cols; i++) {
- for (int j=0; j<rows; j++) {
- palette[i][j].display();
- checkMouseOver(palette[i][j]);
- }
- }
- }
- void mouseDragged() {
- if (mouseButton == LEFT) {
- noFill();
- stroke(f1);
- strokeWeight(20);
- strokeJoin(ROUND);
- line(mouseX,mouseY,pmouseX,pmouseY);
- }
- if (mouseButton == RIGHT) {
- noFill();
- stroke(f2);
- strokeWeight(20);
- strokeJoin(ROUND);
- line(mouseX,mouseY,pmouseX,pmouseY);
- }
- }
- void checkMouseOver(Cell c) {
- if (mousePressed) {
- if (mouseButton == LEFT && mouseX > c.x && mouseX < c.x+c.w && mouseY > c.y && mouseY < c.y+c.h) {
- f1 = (get(mouseX,mouseY));
- }else if (mouseButton == RIGHT && mouseX > c.x && mouseX < c.x+c.w && mouseY > c.y && mouseY < c.y+c.h) {
- f2 = (get(mouseX,mouseY));
- }
- }
- }
- class Cell {
- float x,y,w,h;
- int c;
- Cell (float tempX, float tempY, float tempW, float tempH, int tempC) {
- x = tempX;
- y = tempY;
- w = tempW;
- h = tempH;
- c = tempC;
- }
- void display() {
- stroke(0);
- strokeWeight(3);
- fill(rectCol[c]);
- rect(x,y,w,h);
- }
- }
- Ball[] balls = new Ball[numBalls];
- color[] palette=new color[3];
- void setup(){
- size(screen.width, screen.height);
- noStroke();
- smooth();
- palette[0]=color(0,255,255,200);
- palette[1]=color(255,0,255,200);
- palette[2]=color(255,255,0,200);
- for (int i = 0; i < numBalls; i++) {
- balls[i] = new Ball(random(width), random(height), random(20, 40),int(random(3)), i, balls);
- }
- }
- void draw() {
- background(0);
- for (int i = 0; i < numBalls; i++) {
- balls[i].collide();
- balls[i].move();
- balls[i].display();
- }
- ellipse(mouseX,mouseY,30,30);
- }
- class Ball {
- float x, y;
- float diameter;
- float vx = 0;
- float vy = 0;
- int index;
- int id;
- Ball[] others;
- Ball(float xin, float yin, float din, int indexin, int idin, Ball[] oin) {
- x = xin;
- y = yin;
- diameter = din;
- index = indexin;
- id = idin;
- others = oin;
- }
- void display() {
- fill(palette[index]);
- ellipse(x, y, diameter, diameter);
- }
- }
Oh and a little off topic, I have another post about controlP5 and have found nothing related to what I'm asking in that post and have had no replies... I started the topic in february... so I'd really appreciate the help. Here's the link: ControlP5 Post
Thanks for all the help,
TheGamersHaven
1