Skip to content

Commit

Permalink
Fixed BS
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Aldea authored and Andrei Aldea committed May 17, 2017
1 parent f82e262 commit 8198bdf
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 337 deletions.
268 changes: 173 additions & 95 deletions processingLED.pde
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
/** Example of selecting a particular Serial Port
* from the list of all possibilities returned
* in Serial.list().
*
* Uses showInputDialog box kindly suggested to
* me by GoToLoop. Thank you, GoToLoop!
*/
int
ColorPickerX, //color picker horizontal position
ColorPickerY, //color picker vertical position
LineY, //hue line vertical position
CrossX, //saturation+brightness cross horizontal position
CrossY, //saturation+brightness cross horizontal position
ColorSelectorX = 100, //color selector button horizontal position <------------------------------------------- CHANGE
ColorSelectorY = 100; //color selector button vertical position <------------------------------------------- CHANGE

boolean
isDraggingCross = false, //check if mouse is dragging the cross
isDraggingLine = false, //check if mouse is dragging the line
ShowColorPicker = false; //toggle color picker visibility (even = not visible, odd = visible)

color
activeColor = color(100, 100, 100), //contain the selected color
interfaceColor = color(255); //change as you want <------------------------------------------- CHANGE

import processing.serial.*;
import static javax.swing.JOptionPane.*;

int baud = 19200;
int baud = 9600;

Serial myPort; // The serial port
Serial myPort; // Create object from Serial class

final boolean debug = true;

HScrollbar hs1; //A scrollbar
void setup()

void setup() {
{

size(800, 200);
noStroke();
surface.setResizable(false); //This doesn't work right now but I want it to eventually
size( 700, 700 );
smooth();
surface.setResizable(true); //This doesn't work properly rn but maybe in the future.

colorMode(HSB);

hs1 = new HScrollbar(0, height-8, width, 16, 16);
ColorPickerX = constrain( ColorSelectorX + 40, 10, width - 340 ); //set color picker x position to color selector + 40 and avoid it to be out of screen
ColorPickerY = constrain( ColorSelectorY + 40, 10, height - 300 ); //set color picker y position to color selector + 40 and avoid it to be out of screen

LineY = ColorPickerY + int(hue(activeColor)); //set initial Line position
CrossX = ColorPickerX + int(saturation(activeColor)); //set initial Line position
CrossY = ColorPickerY + int(brightness(activeColor)); //set initial Line position

String COMx, COMlist = "";
/*
Other setup code goes here - I put this at
Expand Down Expand Up @@ -63,102 +80,163 @@ void setup() {
}
}

String val;

void draw() {
background(255);

while (myPort.available() > 0) {
String inBuffer = myPort.readString();
if (inBuffer != null) {
println(inBuffer);
void draw()

{

background( 0 );

//drawColorSelector();
drawColorPicker();
drawLine();
drawCross();
drawActiveColor();
drawValues();
drawOK();

checkMouse();

activeColor = color( LineY - ColorPickerY, CrossX - ColorPickerX, 255 - ( CrossY - ColorPickerY ) ); //set current active color
}


void drawColorSelector()

{

stroke( interfaceColor );
strokeWeight( 1 );
fill( 0 );
rect( ColorSelectorX, ColorSelectorY, 20, 20 ); //draw color selector border at its x y position

stroke( 0 );

if (mouseX>ColorSelectorX&&mouseX<ColorSelectorX+20&&mouseY>ColorSelectorY&&mouseY<ColorSelectorY+20)
fill( hue(activeColor), saturation(activeColor), brightness(activeColor)+30 );
else
fill( activeColor );

rect( ColorSelectorX + 1, ColorSelectorY + 1, 18, 18 ); //draw the color selector fill 1px inside the border
}

String message;

void drawOK()
{
if ( mouseX > ColorPickerX + 285 && mouseX < ColorPickerX + 305 && mouseY > ColorPickerY + 240 && mouseY < ColorPickerY + 260 ) { //check if the cross is on the darker color
fill(0); //optimize visibility on ligher colors
if (mousePressed == true) {
println("OK");
message = (str(red(activeColor)) + "," + str(green(activeColor)) + "," + str(blue(activeColor))); //compose message to set coor
myPort.write(message);
delay(100); //Debounce
}
} else {
fill(100); //optimize visibility on darker colors
}

fill(255);

// Get the position of the img2 scrollbar
// and convert to a value to display the img2 image
fill(255);
text( "OK", ColorPickerX + 285, ColorPickerY + 250 );
}


void drawValues()
{
fill( 255 );
fill( 0 );
textSize( 10 );

text( "H: " + int( ( LineY - ColorPickerY ) * 1.417647 ) + "°", ColorPickerX + 285, ColorPickerY + 100 );
text( "S: " + int( ( CrossX - ColorPickerX ) * 0.39215 + 0.5 ) + "%", ColorPickerX + 286, ColorPickerY + 115 );
text( "B: " + int( 100 - ( ( CrossY - ColorPickerY ) * 0.39215 ) ) + "%", ColorPickerX + 285, ColorPickerY + 130 );

text( "R: " + int( red( activeColor ) ), ColorPickerX + 285, ColorPickerY + 155 );
text( "G: " + int( green( activeColor ) ), ColorPickerX + 285, ColorPickerY + 170 );
text( "B: " + int( blue( activeColor ) ), ColorPickerX + 285, ColorPickerY + 185 );

text( hex( activeColor, 6 ), ColorPickerX + 285, ColorPickerY + 210 );
}

void drawCross()

{
if ( brightness( activeColor ) < 90 )
stroke( 255 );
else
stroke( 0 );
line( CrossX - 5, CrossY, CrossX + 5, CrossY );
line( CrossX, CrossY - 5, CrossX, CrossY + 5 );
}

hs1.update();
hs1.display();

void drawLine()
{
stroke(0);
line( ColorPickerX + 259, LineY, ColorPickerX + 276, LineY );
}

class HScrollbar {
int swidth, sheight; // width and height of bar
float xpos, ypos; // x and y position of bar
float spos, newspos; // x position of slider
float sposMin, sposMax; // max and min values of slider
int loose; // how loose/heavy
boolean over; // is the mouse over the slider?
boolean locked;
float ratio;

HScrollbar (float xp, float yp, int sw, int sh, int l) {
swidth = sw;
sheight = sh;
int widthtoheight = sw - sh;
ratio = (float)sw / (float)widthtoheight;
xpos = xp;
ypos = yp-sheight/2;
spos = xpos + swidth/2 - sheight/2;
newspos = spos;
sposMin = xpos;
sposMax = xpos + swidth - sheight;
loose = l;
}

void update() {
if (overEvent()) {
over = true;
} else {
over = false;
}
if (mousePressed && over) {
locked = true;
}
if (!mousePressed) {
locked = false;
}
if (locked) {
newspos = constrain(mouseX-sheight/2, sposMin, sposMax);
}
if (abs(newspos - spos) > 1) {
spos = spos + (newspos-spos)/loose;
}
}

float constrain(float val, float minv, float maxv) {
return min(max(val, minv), maxv);
void drawColorPicker()
{
stroke( interfaceColor );
line( ColorSelectorX + 10, ColorSelectorY + 10, ColorPickerX - 3, ColorPickerY - 3 );
strokeWeight( 1 );
fill( 0 );
rect( ColorPickerX - 3, ColorPickerY - 3, 283, 260 );

loadPixels();
for ( int j = 0; j < 255; j++ ) //draw a row of pixel with the same brightness but progressive saturation
{
for ( int i = 0; i < 255; i++ ) //draw a column of pixel with the same saturation but progressive brightness
set( ColorPickerX + j, ColorPickerY + i, color( LineY - ColorPickerY, j, 255 - i ) );
}
for ( int j = 0; j < 255; j++ )
{
for ( int i = 0; i < 20; i++ )
set( ColorPickerX + 258 + i, ColorPickerY + j, color( j, 255, 255 ) );
}

boolean overEvent() {
if (mouseX > xpos && mouseX < xpos+swidth &&
mouseY > ypos && mouseY < ypos+sheight) {
return true;
} else {
return false;
fill( interfaceColor );
noStroke();
rect( ColorPickerX + 280, ColorPickerY - 3, 45, 261 );
}

void drawActiveColor()
{
fill( activeColor );
stroke( 0 );
strokeWeight( 1 );
rect( ColorPickerX + 282, ColorPickerY - 1, 41, 80 );
}


void checkMouse()
{
if ( mousePressed )
{
if (mouseX>ColorPickerX+258&&mouseX<ColorPickerX+277&&mouseY>ColorPickerY-1&&mouseY<ColorPickerY+255&&!isDraggingCross)
{
LineY=mouseY;
isDraggingLine = true;
}
}
if (mouseX>ColorPickerX-1&&mouseX<ColorPickerX+255&&mouseY>ColorPickerY-1&&mouseY<ColorPickerY+255&&!isDraggingLine)
{

void display() {
noStroke();
fill(204);
rect(xpos, ypos, swidth, sheight);
if (over || locked) {
fill(0, 0, 0);
} else {
fill(102, 102, 102);
CrossX=mouseX;
CrossY=mouseY;
isDraggingCross = true;
}
rect(spos, ypos, sheight, sheight);
}

float getPos() {
// Convert spos to be values between
// 0 and the total width of the scrollbar
return spos * ratio;
if (mouseX>ColorSelectorX&&mouseX<ColorSelectorX+20&&mouseY>ColorSelectorY&&mouseY<ColorSelectorY+20)
ShowColorPicker = true;

if (mouseX>ColorPickerX+285&&mouseX<ColorPickerX+305&&mouseY>ColorPickerY+240&&mouseY<ColorPickerY+260)
ShowColorPicker = false;
} else

{
isDraggingCross = false;
isDraggingLine = false;
}
}
Loading

0 comments on commit 8198bdf

Please sign in to comment.