Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
6.3 kB
3
Indexable
Never
#ifndef COLOR_PICKER_H
#define COLOR_PICKER_H

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

class ColorPicker{
    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;
    Color currentColor;

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);
            }
        } 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);
            }
        } 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);
            }
        }
        setCustomColor();
    }

    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);
            }
        } 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);
            }
        } 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);
            }
        }
        setCustomColor();
    }

    void draw(){
        // outter box that encapsulates all 3 sliders
        glColor3f(0.8, 0.8, 0.8);
        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(0.8, 0.8, 0.8);
        glBegin(GL_POLYGON);
            glVertex2f(x, y);
            glVertex2f(x+width, y);
            glVertex2f(x+width, y-0.05);
            glVertex2f(x, y-0.05);
        glEnd();

        glColor3f(0.8, 0.8, 0.8);
        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(0.8, 0.8, 0.8);
        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, 0, 0);
        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;

        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();

        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 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;
    }

    // changing the color
    void setCustomColor(){
        currentColor = Color(rColorValue, gColorValue, bColorValue);
    }

    Color getCurrentColor(){
        return currentColor;
    }

    float getR(){
        return rColorValue;
    }

    float getG(){
        return gColorValue;
    }

    float getB(){
        return bColorValue;
    }
};

#endif
Leave a Comment