dentonpotter

Color Variation by Coordinates

Aug 14th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. /*
  2.  * Creative Coding
  3.  * Week 2 Off piste
  4.  * by Karen Cropper
  5.  * Licenced C-BY 4.0 https://creativecommons.org/licenses/by/4.0/
  6.  
  7.  * This program demonstrates coding with color.
  8.  * Press 's' to save your drawing as an image to the file "color.jpg"
  9.  * Press 'r' to erase your drawing and start with a blank screen
  10.  *
  11.  */
  12.  
  13.  
  14.  
  15. // Setting up some variables to use further down
  16.   int colorBack; //for background color initially and to clear
  17.   float xVar; // to scale the colour in x
  18.   float yVar; // to scale the colour in y  
  19.  
  20.  
  21. // setup function -- called once when the program begins
  22. void setup() {
  23.  
  24.   // set the display window size
  25.   size(1200, 675); // width and height relate to these two values
  26.  
  27.   // set the background colour to white
  28.   colorBack = 255; // initialising background color also used in reset
  29.   background(colorBack);
  30.  
  31. }
  32.  
  33. // draw function
  34. void draw() {
  35.    
  36.    if (mousePressed) {
  37.  
  38. //  strokeWeight(random(2)); // random thickness of line
  39.     stroke(0); // set the stroke colour black
  40.    
  41.     xVar = mouseX*255/width;
  42.     yVar = mouseY*255/height;
  43.  
  44. //   had this in to check what was being output by the variable calculation    
  45. //   print("x = "+xVar+", y = "+yVar+" :   ");
  46.    
  47. //  This is varying the red and the green by position X and Y but with
  48. //  blue fixed at a value. Some degree of transparency is nicer
  49.    
  50.     fill(xVar, yVar, 0, 150); // set the fill colour with transparency
  51.  
  52.     // draw a rectangle with a size variation in size
  53.     rect(mouseX, mouseY, random(10,50), random(10,50));
  54.   }
  55.    
  56.  
  57.    // save your drawing when you press keyboard 's'
  58.   if (keyPressed == true && key=='s') {
  59.     saveFrame("color.jpg");
  60.   }
  61.  
  62.   // erase your drawing when you press keyboard 'r'
  63.   if (keyPressed == true && key == 'r') {
  64.     background(colorBack);
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment