Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
6.9 kB
2
Indexable
Never
#ifndef COLOR_PICKER_H
#define COLOR_PICKER_H

#include <GL/freeglut.h>
#include <GL/gl.h>
#include "BuildTools/Color.h"
#include "ColorSelector.h"

#include <iostream>
using namespace std;

class ColorPicker : public ColorSelector {
    float x;
    float y;
    float height;
    float width;
    float middle;
    float r_slider_x;
    float b_slider_x;
    float g_slider_x;
    float rColorValue;
    float gColorValue;
    float bColorValue;
    
public:

    ColorPicker(){
        x = 0;
        y = 0;
        height = 0.2;
        width = 0.6;
        r_slider_x = 0;
        b_slider_x = 0;
        g_slider_x = 0;
        rColorValue = 0;
        gColorValue = 0;
        bColorValue = 0;
    }

    ColorPicker(float x, float y){
        this->r_slider_x = x;
        this->g_slider_x = x;
        this->b_slider_x = x;
        this->x = x;
        this->y = y;
        height = 0.2;
        width = 0.6;
    }

    ColorPicker(float x, float y, float w, float h){
        this->r_slider_x = x;
        this->g_slider_x = x;
        this->b_slider_x = x;
        this->x = x;
        this->y = y;
        this->height = h;
        this->width = w;
    }

    void handleMouseClick(float mx, float my, char letter){
        if (letter == 'g'){
            if (mx <= (x + width) - 0.02 && mx >= x + 0.019){
                g_slider_x = mx-0.025;
                gColorValue = ((g_slider_x+0.01) - x)/((x+width-0.05) - x);
            }  
            markerColor = Color(rColorValue, gColorValue, bColorValue);

        } else if (letter == 'b'){
            if (mx <= (x + width) - 0.02 && mx >= x + 0.019){
                b_slider_x = mx-0.025;
                bColorValue = ((b_slider_x+0.01) - x)/((x+width-0.05) - x);
            }
            markerColor = Color(rColorValue, gColorValue, bColorValue);
            
        } else if (letter == 'r'){
            if (mx <= (x + width) - 0.02 && mx >= x + 0.019){
                r_slider_x = mx-0.025;
                rColorValue = ((r_slider_x+0.01) - x)/((x+width-0.05) - x);
            }
            markerColor = Color(rColorValue, gColorValue, bColorValue);
            
        }
    }

    void handleMouseMotion(float mx, float my, char letter){
        if (letter == 'g'){
            if (mx <= (x + width) - 0.02 && mx >= x + 0.019){
                g_slider_x = mx-0.025;
                gColorValue = ((g_slider_x+0.01) - x)/((x+width-0.05) - x);
            }
            markerColor = Color(rColorValue, gColorValue, bColorValue);
        } else if (letter == 'b'){
            if (mx <= (x + width) - 0.02 && mx >= x + 0.019){
                b_slider_x = mx-0.025;
                bColorValue = ((b_slider_x+0.01) - x)/((x+width-0.05) - x);
            }
            markerColor = Color(rColorValue, gColorValue, bColorValue);
        } else if (letter == 'r'){
            if (mx <= (x + width) - 0.02 && mx >= x + 0.019){
                r_slider_x = mx-0.025;
                rColorValue = ((r_slider_x+0.01) - x)/((x+width-0.05) - x);
            }
            markerColor = Color(rColorValue, gColorValue, bColorValue);
        }
    }

    void draw(){
        // outter box that encapsulates all 3 sliders
        glColor3f(1, 1, 1);
        glBegin(GL_POLYGON);
            glVertex2f(x, y);
            glVertex2f(x+width, y);
            glVertex2f(x+width, y-height);
            glVertex2f(x, y-height);
        glEnd();

        // bounding boxes so you can click around slider box
        glColor3f(1, 1, 1);
        glBegin(GL_POLYGON);
            glVertex2f(x, y);
            glVertex2f(x+width, y);
            glVertex2f(x+width, y-0.05);
            glVertex2f(x, y-0.05);
        glEnd();

        glColor3f(1, 1, 1);
        glBegin(GL_POLYGON);
            glVertex2f(x, y-middle);
            glVertex2f(x+width, y-middle);
            glVertex2f(x+width, y-0.05-middle);
            glVertex2f(x, y-0.05-middle);
        glEnd();

        glColor3f(1, 1, 1);
        glBegin(GL_POLYGON);
            glVertex2f(x, y-height+0.05);
            glVertex2f(x+width, y-height+0.05);
            glVertex2f(x+width, y-height);
            glVertex2f(x, y-height);
        glEnd();

        // slider "rails"
        float halfBoxHeight = 0.025;

        glColor3f(0, 0, 0);
        glBegin(GL_LINES);
            glVertex2f(x, y-halfBoxHeight);
            glVertex2f(x+width, y-halfBoxHeight);

            glVertex2f(x, y-(height/2));
            glVertex2f(x+width, y-(height/2));

            glVertex2f(x, y-height + halfBoxHeight);
            glVertex2f(x+width, y-height + halfBoxHeight);
        glEnd();

        // slider boxes
        glColor3f(0.710, 0.008, 0.008);
        glBegin(GL_POLYGON);
            glVertex2f(r_slider_x, y);
            glVertex2f(r_slider_x+0.05, y);
            glVertex2f(r_slider_x+0.05, y-0.05);
            glVertex2f(r_slider_x, y-0.05);
        glEnd();

        middle = (height/2)-0.025;

        glColor3f(0.039, 0.710, 0);
        glBegin(GL_POLYGON);
            glVertex2f(g_slider_x, y-middle);
            glVertex2f(g_slider_x+0.05, y-middle);
            glVertex2f(g_slider_x+0.05, y-0.05-middle);
            glVertex2f(g_slider_x, y-0.05-middle);
        glEnd();

        glColor3f(0.008, 0.180, 0.620);
        glBegin(GL_POLYGON);
            glVertex2f(b_slider_x, y-height+0.05);
            glVertex2f(b_slider_x+0.05, y-height+0.05);
            glVertex2f(b_slider_x+0.05, y-height);
            glVertex2f(b_slider_x, y-height);
        glEnd();
    }

    // inside function for outer bounding box
    bool outerBoxInside(float mx, float my){
        return mx >= x && mx <= (x+width) && my <= y && my >= (y-height);
    }

    // inside functions for slider buttons
    bool insideButtonR(float mx, float my){
        return mx >= r_slider_x && mx <= (r_slider_x+0.05) && my <= y && my >= (y-0.5);
    }
    bool insideButtonG(float mx, float my){
        return mx >= g_slider_x && mx <= (g_slider_x+0.05) && my <= y-middle && my >= (y-0.5-middle);
    }
    bool insideButtonB(float mx, float my){
        return mx >= b_slider_x && mx <= (b_slider_x+0.05) && my <= (y-height+0.05) && my >= (y-height);
    }

    // inside function for slider bounding boxes
    bool insideBoundingBoxR(float mx, float my){
        return mx >= x && x <= (x+width) && my <= y && my >= y-0.05;
    }

    bool insideBoundingBoxG(float mx, float my){
        return mx >= x && x <= (x+width) && my <= y-middle && my >= y-0.05-middle;
    }

    bool insideBoundingBoxB(float mx, float my){
        return mx >= x && x <= (x+width) && my <= y-height+0.05 && my >= y-height;
    }

    // color and getter functions
    Color getCurrentColor(){
        return markerColor;
    }

    float getR(){
        return rColorValue;
    }

    float getG(){
        return gColorValue;
    }

    float getB(){
        return bColorValue;
    }
};

#endif
Leave a Comment