Ok, one last problem. I managed to get the sketch to work without lerpColor() because I don't think that exists in JavaScript and I'll want to do the same there later. But I used map() to get my sketch to work which apparently also doesn't exist in JavaScript (or is not supported in all browsers). Can you help me figure out how to assign val its value without using map()? I can't seem to figure it out. I highlighted the line in red.
On a side note, I'm using int instead of color to make the future translating to JavaScript easier. The sketch is in black and white again to avoid having to do all of my calculations for each color channel for now.
One more side note, I noticed with the sketch you wrote above that it doesn't do the gradients correctly if the nodes pass one another so I changed the code for finding the closest left and right nodes.
- int numNodes = 5;
- int[] nodeColor = new int[numNodes];
- int[] nodePos = new int[numNodes];
- boolean somethingSelected;
- int selectedNode;
- void setup() {
- size(800, 200, P2D);
- for (int i = 0; i < numNodes; i++) {
- nodeColor[i] = (int)random(256);
- nodePos[i] = width/(numNodes-1)*i;
- }
- println("Click and drag the nodes along the line");
- println("Press 'c' to randomize the color of the nodes");
- println("Press 'p' to reset the position of the nodes");
- }
- void draw() {
- // Display Hand if mouse is on a draggable node
- boolean onNode = false;
- for (int i = 1; i < numNodes-1; i++) {
- if (mouseX > nodePos[i]-5 && mouseX < nodePos[i]+5 && mouseY > 25 && mouseY < 75) {
- onNode = true;
- break;
- }
- }
- if (onNode) cursor(HAND);
- else cursor(ARROW);
- // Move selected node
- if (somethingSelected) nodePos[selectedNode] = mouseX;
- // Draw
- background(255);
- line(0, 50, width, 50);
- for (int i = 0; i < numNodes; i++) {
- fill(nodeColor[i]);
- rect(nodePos[i]-5, 25, 10, 50);
- }
- loadPixels();
- for (int i = 0; i < width; i++) {
- // Closest left node info
- int leftColor = nodeColor[0];
- int leftDist = 2147483647; // Infinity
- int leftPos = nodePos[0];
-
- // Closest right node info
- int rightColor = nodeColor[numNodes-1];
- int rightDist = 2147483647; // Infinity
- int rightPos = nodePos[numNodes-1];
-
- // Get closest node positions and color
- for (int j = 0; j < numNodes; j++) {
- int currDist = abs(i-nodePos[j]);
- if (i >= nodePos[j] && leftDist > currDist) {
- leftColor = nodeColor[j];
- leftDist = currDist;
- leftPos = nodePos[j];
- }
- if (i < nodePos[j] && rightDist > currDist) {
- rightColor = nodeColor[j];
- rightDist = currDist;
- rightPos = nodePos[j];
- }
- }
-
- // Assign a color based on closest node position and color
- float percent = float(i-leftPos)/(rightPos-leftPos);
- int val = (int)map(percent, 0, 1, leftColor, rightColor);
- for (int j = height/2; j < height; j++) pixels[i+j*width] = (val<<16)+(val<<8)+val;
- }
- updatePixels();
- }
- void mousePressed() {
- somethingSelected = false;
- for (int i = 0; i < numNodes; i++) {
- if (mouseX > nodePos[i]-5 && mouseX < nodePos[i]+5 && mouseY > 25 && mouseY < 75) {
- somethingSelected = true;
- selectedNode = i;
- break;
- }
- }
- }
- void mouseReleased() {
- somethingSelected = false;
- }
- void keyPressed() {
- if (key == 'c' || key == 'C') {
- for (int i = 0; i < numNodes; i++) nodeColor[i] = (int)random(256);
- }
- if (key == 'p' || key == 'P') {
- for (int i = 0; i < numNodes; i++) nodePos[i] = width/(numNodes-1)*i;
- }
- }