Slider Math
in
Programming Questions
•
6 months ago
Hello,
I am making a slider that changes the tint of an image. I looked some of the code from here
http://processing.org/learning/topics/scrollbar.html. However i want the slider to change according to a given min and max value in the constructor. In this case 0 to 255, and the slider needs to be relative to the screen size. This is how i did it, the screen size of 600 is the same as the max value of 255. Now 255 is the same as 100% of the width. Now i calculated this with a decrease ratio to get the same value of 255. However the calculation acts in a sort of Fibonacci style :). My question is can this be done with min() and max(). The slider need to be relative to the screen and the min, max values need to be 100%.
Thanks
- PImage img;
- Scroller scroll;
- Scroller scroll1;
- void setup(){
- size(600, 600);
- img = loadImage("lake.jpg");
- scroll = new Scroller(0, 0, 32, 32);
- scroll1 = new Scroller(0, 40, 32, 32);
- }
- void draw(){
- background(255);
- scroll.display();
- scroll.setBackground();
- scroll.setHover();
- scroll1.display();
- scroll1.setBackground();
- scroll1.setHover();
- tint(scroll.calculate(0, 255), scroll1.calculate(0, 255));
- image(img, 0, 80);
- }
- class Scroller{
- float xPos, yPos;
- float sliderWidth, sliderHeight;
- float sliderMaxBorder, sliderMinBorder;
- boolean over;
- boolean locked;
- float m;
- Scroller(float tempX, float tempY, float tempWidth,
- float tempHeight){
- xPos = tempX;
- yPos = tempY;
- sliderWidth = tempWidth;
- sliderHeight = tempHeight;
- sliderMaxBorder = width-sliderWidth;
- sliderMinBorder = 0;
- }
- void setHover(){
- if(overEvent()){
- println("Hover = " + over);
- over = true;
- }
- else{
- println("Hover = " + over);
- over = false;
- }
- if(mousePressed && over){
- println("Clicked = " + locked);
- locked = true;
- }
- else{
- println("Clicked = " + locked);
- locked = false;
- }
- if(locked){
- xPos = constrain(mouseX-sliderHeight/2,
- sliderMinBorder, sliderMaxBorder);
- }
- }
- float calculate(float tempMin, float tempMax)
- {
- float minValue = tempMin;
- float maxValue = tempMax;
- float a = getPos() / 100;
- float b = maxValue / 100;
- float ca = a / b;
- float aa = ((getPos() / 10) * ca) * 2;
- println(aa);
- return aa;
- }
- void setBackground(){
- fill(80, 80, 80, 150);
- //rectMode(CENTER);
- rect(0, yPos + sliderHeight/3, width, sliderHeight/3);
- }
- boolean overEvent(){
- if(mouseX > xPos && mouseX < xPos + sliderWidth &&
- mouseY > yPos && mouseY < yPos + sliderHeight){
- return true;
- }
- else{
- return false;
- }
- }
- void display(){
- noStroke();
- fill(204);
- if(over){
- fill(0, 0, 0);
- }
- else{
- fill(102, 102, 102);
- }
- rect(xPos, yPos, sliderHeight, sliderHeight);
- }
- float getPos(){
- return xPos;
- }
- }
1