User:Marlon/RGB

From XPUB & Lens-Based wiki

RGB Avatar

Identicons are hash values visualized. These values are often IP addresses, from which avatars are generated.

Inspired by Identicons, this tool uses pressure sensors to change the RGB values of the graphical output! Mouse-click screenshots!

Samples

Rgbavatar1.pngRgbavatar5.pngRgbavatar6.pngRgbavatar3.pngRgbavatar4.pngRgbavatar9.pngRgbavatar7.pngRgbavatar8.png

Source code:

import processing.serial.*;
// import gifAnimation.*;
// Gif loopingGif;
 
Serial myPort;
int linefeed = 10;   // Linefeed in ASCII
int numSensors = 3;  // we will be expecting for reading data from four sensors
int sensors[];       // array to read the 3 values
int pSensors[];      // array to store the previous reading, useful
// for comparing
// actual reading with the last one


int num = 5;
int bgCol = 255;


float rot;

void setup() {
  size(250, 250);
    frameRate(80);
  // List all the available serial ports in the output pane.
  // You will need to choose the port that the Wiring board is
  // connected to from this list. The first port in the list is
  // port #0 and the third port in the list is port #2.
  println(Serial.list());

  myPort = new Serial(this, Serial.list()[0], 115200);
  // read bytes into a buffer until you get a linefeed (ASCII 10):
  myPort.bufferUntil(linefeed);
  
      background(bgCol);
  
 
}

int xdest = 0;
int ydest = 0;

void draw() {
  rot += PI/15;
  
  translate(width/2, height/2);
  rotate(rot);
  
  int xstep;
  if (xdest == 0) {
    xstep = 0; 
    xdest = int(random(width) - (width/2));
  } else if (xdest > 0) {   
    xstep = 1; 
    xdest--;
  } else {
    xstep = -1; 
    xdest++;
  }
  
  int ystep;
  if (ydest == 0) {
    ystep = 0; 
    ydest = int(random(height) - (height/2));
  } else if (ydest > 0) {   
    ystep = 1; 
    ydest--;
  } else {
    ystep = -1; 
    ydest++;
  }
   
  drawStuff();
  // pixelShift(-2,-2);
  pixelShift(xstep,ystep);
}

int trans = 255;

void drawStuff() {

  
    if((pSensors != null)&&(sensors != null)) {

    for(int i=0; i < numSensors; i++) {
      int f = sensors[i] - pSensors[i];  // actual - previous value
      

      float r = map(sensors[0], 0, 1023, 0, 255);
      float g = map(sensors[1], 0, 1023, 0, 255);
      float b = map(sensors[2], 0, 1023, 0, 255);
      
        
  fill(r, g, b, trans);
  stroke(255,255,255, trans);
  rect(0,0,50,50);
  noStroke();
  fill(r, g, b, trans);
  rect(2,2,46,46);
  // stroke(255,0,0);
  
  rect(4,4,42,42);
  
      if(f > 0) {
       println("sensor "+i+" increased by "+f);  // value increased
      }
      if(f < 0) {
        println("sensor "+i+" decreased by "+f);  // value decreased
      }
  
}
    }
}


void keyPressed() {
  if (trans == 0) {
    trans = 255;
  } else {
    trans = 0;
  }
}


void mouseReleased() {
  saveFrame(gimme_name("myProject_"));
};
 
String gimme_name(String begin)
{
  String name = begin;
  name+=String.valueOf(year());
  name+=".";
  if(month()<10) name+="0";
  name+=String.valueOf(month());
  name+=".";
  if(day()<10) name+="0";
  name+=String.valueOf(day());
  name+="_";
  if(hour()<10) name+="0";
  name+=String.valueOf(hour());
  name+=".";
  if(minute()<10) name+="0";
  name+=String.valueOf(minute());
  name+=".";
  if(second()<10) name+="0";
  name+=String.valueOf(second());
  name+=".png";
  return name;
}
 



void pixelShift(int xshift, int yshift) {
  // copy screen into an array
  color transArr[] = new color[width * height];
  
  loadPixels();
  arraycopy(pixels, transArr);
  
   for (int y=1; y < height; y++) {
      for (int x=1; x < width; x++){
        
        if ((x+xshift < width) && (x+xshift > 0)) {
          if ((y+yshift < height) && (y+yshift > 0)) {
            pixels[x + (y*width)] = transArr[(x+xshift)+ ((y+yshift)*width)];
          }
        }
        
      }
    }
  updatePixels();
  
}

void serialEvent(Serial myPort) {

  // read the serial buffer:
  String myString = myPort.readStringUntil(linefeed);

  // if you got any bytes other than the linefeed:
  if (myString != null) {

    myString = trim(myString);

    // split the string at the commas
    // and convert the sections into integers:

    pSensors = sensors;
    sensors = int(split(myString, ','));

    // print out the values you got:

    for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
      print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
    }

    // add a linefeed after all the sensor values are printed:
    println();
  
  }
}