-
Notifications
You must be signed in to change notification settings - Fork 2
/
HSLPickWidget.cpp
128 lines (106 loc) · 3.34 KB
/
HSLPickWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//-------------------------------------------------------------------------
//
// Joe Kniss
// HSL Color Space Picker Widget
// HSLPickWidget.cpp : rotates a HSL ball and returns a color
// 3-20-01
// ________ ____ ___
// | \ / | / /
// +---+ \/ |/ /
// +--+| |\ /| <
// | || | \ / | |\ \
// | | \/ | | \ \
// \_____| |__| \__\
// Copyright 2001
// Joe Michael Kniss
// "All Your Base are Belong to Us"
//-------------------------------------------------------------------------
#ifdef WIN32
#include <windows.h>
#endif
#include "HSLPickWidget.h"
#include "gluvv.h"
#include <stdio.h>
#include <GL/glut.h>
#include "VectorMath.h"
//======================================================== HSLPickWidget
//======================================================================
HSLPickWidget::HSLPickWidget(gluvvGlobal *gluvv)
{
hue = .3;
sat = 1;
lev = .5;
}
HSLPickWidget::HSLPickWidget(HSLPickWidget &pw, gluvvGlobal *gluvv)
{
hue = pw.hue;
sat = pw.sat;
lev = pw.lev;
}
//======================================================= ~HSLPickWidget
//======================================================================
HSLPickWidget::~HSLPickWidget()
{
}
//=============================================================== update
//======================================================================
void HSLPickWidget::updateHL(float dh, float dl)
{
hue = (hue+dh)> 1.0 ? (hue+dh-1.0) : ((hue+dh)< 0 ? (hue+dh+1.0) : (hue+dh));
lev = (lev+dl) > 1 ? 1 : (lev+dl) < 0 ? 0 : (lev+dl);
}
void HSLPickWidget::updateS(float ds)
{
lev = (sat+ds) > 1 ? 1 : (sat+ds) < 0 ? 0 : (sat+ds);
}
//============================================================ get color
//======================================================================
void HSLPickWidget::getColor(float col[3]){
float m1, m2, fract, mid1, mid2;
int sextant;
float H = hue;
float S = sat;
float L = lev;
if (S == 0) {
col[0] = col[1] = col[2] = L;
return;
}
/* else there is hue */
if (L <= 0.5)
m2 = L*(1+S);
else
m2 = L + S - L*S;
m1 = 2*L - m2;
if (1.0 == H)
H = 0;
H *= 6;
sextant = (int) floor(H);
fract = H - sextant;
mid1 = m1 + fract*(m2 - m1);
mid2 = m2 + fract*(m1 - m2);
/* compared to HSVtoRGB: V -> m2, min -> m1 */
switch (sextant) {
case 0: { col[0] = m2; col[1] = mid1; col[2] = m1; break; }
case 1: { col[0] = mid2; col[1] = m2; col[2] = m1; break; }
case 2: { col[0] = m1; col[1] = m2; col[2] = mid1; break; }
case 3: { col[0] = m1; col[1] = mid2; col[2] = m2; break; }
case 4: { col[0] = mid1; col[1] = m1; col[2] = m2; break; }
case 5: { col[0] = m2; col[1] = m1; col[2] = mid2; break; }
}
}
//===================================================== set color
//===============================================================
void HSLPickWidget::setColor(float h, float s, float l)
{
hue = h;
sat = s;
lev = l;
}
//========================================================= reset
//===============================================================
void HSLPickWidget::reset(float h, float s, float l)
{
hue = h;
lev = l;
sat = s;
}