#javascript-processing-example-basic-color-lineargradient.htm / htm
<!DOCTYPE html> <html><head> <script src="javascript-processing-example-processing.js"></script> <script src="javascript-processing-example-init.js"></script> <link rel="stylesheet" href="javascript-processing-example-style.css"> </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1> <h2>LinearGradient</h2> <p>by Ira Greenberg. Using the convenient red(), green() and blue() component functions, generate some linear gradients.</p> <p><a href="http://processing.org/learning/basics/lineargradient.html"><b>Original Processing.org Example:</b> LinearGradient</a><br> <script type="application/processing"> // constants int Y_AXIS = 1; int X_AXIS = 2; void setup(){ size(200, 200); // create some gradients // background color b1 = color(190, 190, 190); color b2 = color(20, 20, 20); //setGradient(0, 0, width, height, b1, b2, Y_AXIS); //center squares color c1 = color(255, 120, 0); color c2 = color(10, 45, 255); color c3 = color(10, 255, 15); color c4 = color(125, 2, 140); color c5 = color(255, 255, 0); color c6 = color(25, 255, 200); setGradient(25, 25, 75, 75, c1, c2, Y_AXIS); setGradient(100, 25, 75, 75, c3, c4, X_AXIS); setGradient(25, 100, 75, 75, c2, c5, X_AXIS); setGradient(100, 100, 75, 75, c4, c6, Y_AXIS); } void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ){ // calculate differences between color components float deltaR = red(c2)-red(c1); float deltaG = green(c2)-green(c1); float deltaB = blue(c2)-blue(c1); // choose axis if(axis == Y_AXIS){ /*nested for loops set pixels in a basic table structure */ // column for (int i=x; i<=(x+w); i++){ // row for (int j = y; j<=(y+h); j++){ color c = color( (red(c1)+(j-y)*(deltaR/h)), (green(c1)+(j-y)*(deltaG/h)), (blue(c1)+(j-y)*(deltaB/h)) ); set(i, j, c); } } } else if(axis == X_AXIS){ // column for (int i=y; i<=(y+h); i++){ // row for (int j = x; j<=(x+w); j++){ color c = color( (red(c1)+(j-x)*(deltaR/h)), (green(c1)+(j-x)*(deltaG/h)), (blue(c1)+(j-x)*(deltaB/h)) ); set(j, i, c); } } } } </script><canvas width="200" height="200"></canvas></p> <div style="overflow: hidden; height: 0px; width: 0px;"></div> <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a> // unless otherwise stated.</b> // constants int Y_AXIS = 1; int X_AXIS = 2; void setup(){ size(200, 200); // create some gradients // background color b1 = color(190, 190, 190); color b2 = color(20, 20, 20); setGradient(0, 0, width, height, b1, b2, Y_AXIS); //center squares color c1 = color(255, 120, 0); color c2 = color(10, 45, 255); color c3 = color(10, 255, 15); color c4 = color(125, 2, 140); color c5 = color(255, 255, 0); color c6 = color(25, 255, 200); setGradient(25, 25, 75, 75, c1, c2, Y_AXIS); setGradient(100, 25, 75, 75, c3, c4, X_AXIS); setGradient(25, 100, 75, 75, c2, c5, X_AXIS); setGradient(100, 100, 75, 75, c4, c6, Y_AXIS); } void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ){ // calculate differences between color components float deltaR = red(c2)-red(c1); float deltaG = green(c2)-green(c1); float deltaB = blue(c2)-blue(c1); // choose axis if(axis == Y_AXIS){ /*nested for loops set pixels in a basic table structure */ // column for (int i=x; i<=(x+w); i++){ // row for (int j = y; j<=(y+h); j++){ color c = color( (red(c1)+(j-y)*(deltaR/h)), (green(c1)+(j-y)*(deltaG/h)), (blue(c1)+(j-y)*(deltaB/h)) ); set(i, j, c); } } } else if(axis == X_AXIS){ // column for (int i=y; i<=(y+h); i++){ // row for (int j = x; j<=(x+w); j++){ color c = color( (red(c1)+(j-x)*(deltaR/h)), (green(c1)+(j-x)*(deltaG/h)), (blue(c1)+(j-x)*(deltaB/h)) ); set(j, i, c); } } } }</pre> </body></html>
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.