fullscreen
Final_Colorselector.pdecolorSelectorCommands.pdecolorSelectorGuts.pde
color myColor1;
color myColor2;
color myColor3;
void setup() {
size(800, 600);
colorMode(HSB);
setSelectedColor(100, 100, 100);
setupColorSelector(550, 0, 250, 600, 0);
myColor1 = color(random(0, 255), 50, 100);
myColor2 = color(random(0, 255), 200, 255);
myColor3 = color(random(0, 255), 100, 200);
}
void draw() {
background(0);
if (mousePressed) {
colorSelectorHandleMouse(mouseX, mouseY);
}
drawColorSelector();
bakeAPie();
//smaller rectangle to the left
fill(myColor2);
rect(100, 225, 75, 125);
}
void bakeAPie() {
//large left rectangle
fill(myColor1);
rect(0, 0, 300, 600);
//large right rectangle
fill(myColor3);
rect(300, 0, 300, 600);
//right rectangle (able to be changed)
fill(selectedColor);
rect(400, 225, 75, 125);
}
//this variable will be set by the colorSelector.
//You can refer to it in your code.
color selectedColor = color(0,0,0);
/*
this command establishes the colorSelectorProperties:
xPos, yPos, w & h define where it is drawn on the screen.
mode has three valid values: 0, 1, & 2. Try them...
the mode will also change based on Processing's colorMode(RGB -vs- HSB);
*/
void setupColorSelector(int xPos, int yPos, int w, int h, int mode) {
colorSelectorXPosition = xPos;
colorSelectorYPosition = yPos;
colorSelectorWidth = w;
colorSelectorHeight = h;
colorSelectorMode = mode;
}
//this command causes the color selector to be draw on the screen
void drawColorSelector() {
drawColorSelectorAndGuides();
}
/*
this command will compare the coords you pass it
(likely mouseX and mouseY) and cause the color
selector to respond.
*/
void colorSelectorHandleMouse(int x, int y) {
if (coordsInsideColorSelector(x, y)) {
setSelectedColorByCoords(x, y);
}
}
int colorSelectorXPosition = 100;
int colorSelectorYPosition = 0;
int colorSelectorWidth = 400;
int colorSelectorHeight = 60;
float selectedColorProps[] = {0,0,0};
int colorSelectorMode = 0;
boolean coordsInsideColorSelector(int x, int y) {
return ( x >= colorSelectorXPosition &&
x < colorSelectorXPosition + colorSelectorWidth &&
y >= colorSelectorYPosition &&
y < colorSelectorYPosition + colorSelectorHeight);
}
void setSelectedColorByCoords(int x, int y) {
float relativeX = (x - colorSelectorXPosition) / float(colorSelectorWidth);
float relativeY = (y - colorSelectorYPosition) / float(colorSelectorHeight);
if (relativeX > .9) {
//we're in the hue bar
selectedColorProps[(colorSelectorMode + 0) % 3] = relativeY * 255;
}
else {
//we're in the SB box
selectedColorProps[(colorSelectorMode + 1) % 3] = (relativeX / .9) * 255;
selectedColorProps[(colorSelectorMode + 2) % 3] = relativeY * 255;
}
selectedColor = color ( selectedColorProps[0], selectedColorProps[1], selectedColorProps[2]);
}
void setColorSelectorMode(int mode) {
colorSelectorMode = mode;
}
void setSelectedColor(float a, float b, float c) {
selectedColor = color(a, b, c);
selectedColorProps = new float[]{a, b,c};
}
void drawColorSelectorAndGuides() {
float colorValueStepX, colorValueStepY;
float[] colorValue = {0,0,0};
int pixelIndex;
int sbBoxW = int(colorSelectorWidth * .9);
int hueBarW = int(colorSelectorWidth * .1);
colorValueStepY = 255 / float(colorSelectorHeight);
colorValueStepX = 255 / float(sbBoxW);
colorValue[1] = 0;
colorValue[2] = selectedColorProps[colorSelectorMode];
loadPixels();
for (int j=0; j < colorSelectorHeight; j++) {
pixelIndex = (colorSelectorYPosition + j) * width + colorSelectorXPosition;
colorValue[0] = 0;
for (int i=0; i < sbBoxW; i++) {
if (colorSelectorMode == 0 ) {
pixels[pixelIndex] = color(colorValue[2], colorValue[0], colorValue[1]);
} else if (colorSelectorMode == 1) {
pixels[pixelIndex] = color(colorValue[1], colorValue[2], colorValue[0]);
} else {
pixels[pixelIndex] = color(colorValue[0], colorValue[1], colorValue[2]);
}
colorValue[0] += colorValueStepX;
pixelIndex++;
}
colorValue[1] += colorValueStepY;
}
colorValue[1] = 0;
for (int j = 0; j < colorSelectorHeight; j++) {
pixelIndex = (colorSelectorYPosition + j) * width + colorSelectorXPosition + sbBoxW;
for (int i=0; i < hueBarW; i++) {
if (colorSelectorMode == 0 ) {
pixels[pixelIndex] = color(colorValue[1], selectedColorProps[1], selectedColorProps[2]);
} else if (colorSelectorMode == 1 ) {
pixels[pixelIndex] = color(selectedColorProps[0], colorValue[1], selectedColorProps[2]);
} else {
pixels[pixelIndex] = color(selectedColorProps[0], selectedColorProps[1], colorValue[1]);
}
pixelIndex++;
}
colorValue[1] += colorValueStepY;
}
updatePixels();
//draw an indicator of current selection
float selColHue = selectedColorProps[(colorSelectorMode + 0) % 3] / float(255);
float selColSat = selectedColorProps[(colorSelectorMode + 1) % 3] / float(255);
float selColBri = selectedColorProps[(colorSelectorMode + 2) % 3] / float(255);
strokeWeight(1);
noFill();
stroke(255);
rect(colorSelectorXPosition + (colorSelectorWidth * .9) * selColSat - 2,
colorSelectorYPosition + colorSelectorHeight * selColBri - 2,
5, 5);
stroke(0);
rect(colorSelectorXPosition + (colorSelectorWidth * .9) * selColSat - 1,
colorSelectorYPosition + colorSelectorHeight * selColBri - 1,
3, 3);
line(colorSelectorXPosition + (colorSelectorWidth * .9),
colorSelectorYPosition + selColHue * colorSelectorHeight - 1,
colorSelectorXPosition + colorSelectorWidth,
colorSelectorYPosition + selColHue * colorSelectorHeight - 1);
line(colorSelectorXPosition + (colorSelectorWidth * .9),
colorSelectorYPosition + selColHue * colorSelectorHeight + 1,
colorSelectorXPosition + colorSelectorWidth,
colorSelectorYPosition + selColHue * colorSelectorHeight + 1);
stroke(255);
line(colorSelectorXPosition + (colorSelectorWidth * .9),
colorSelectorYPosition + selColHue * colorSelectorHeight - 2,
colorSelectorXPosition + colorSelectorWidth,
colorSelectorYPosition + selColHue * colorSelectorHeight - 2);
line(colorSelectorXPosition + (colorSelectorWidth * .9),
colorSelectorYPosition + selColHue * colorSelectorHeight + 2,
colorSelectorXPosition + colorSelectorWidth,
colorSelectorYPosition + selColHue * colorSelectorHeight + 2);
}