-
Notifications
You must be signed in to change notification settings - Fork 5
/
ColorHSL.java
156 lines (130 loc) · 3.83 KB
/
ColorHSL.java
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.aobuchow.themes.spectrum.preferences;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.PlatformUI;
public class ColorHSL {
private Color color;
private float hue;
private float saturation;
private float luminance;
public ColorHSL(Color color) {
this.color = color;
this.hue = color.getRGB().getHSB()[0];
this.saturation = color.getRGB().getHSB()[1];
this.luminance = color.getRGB().getHSB()[2];
}
public ColorHSL(float hue, float saturation, float luminance) {
this(new Color(PlatformUI.getWorkbench().getDisplay(), new RGB(hue, saturation, luminance)));
}
// TODO: Add test coverage
public ColorHSL modifyProperty(HSL_PROPERTY propertyToModify, BOUND_BEHAVIOR boundBehavior, float amount) {
// Hue must be within the range [0, 360]
// Saturation must be within the range [0, 1]
// Luminance must be within the range [0, 1]
float newValue = -1;
float max = 1;
float min = 0;
switch (propertyToModify) {
case HUE:
max = 360;
newValue = hue;
break;
case SATURATION:
newValue = saturation;
break;
case LUMINANCE:
newValue = luminance;
break;
}
newValue = newValue + amount;
switch (boundBehavior) {
case CYCLE:
if (newValue < min) {
newValue = max - Math.abs(newValue);
} else if (newValue > max) {
newValue = min + (newValue - max);
}
break;
case LIMIT:
newValue = setFloatBounds(newValue, min, max);
break;
case REVERSE:
if (newValue < min) {
newValue = min + Math.abs(newValue);
} else if (newValue > max) {
newValue = max - (newValue - max);
}
}
switch (propertyToModify) {
case HUE:
return new ColorHSL(newValue, this.getSaturation(), this.getLuminance());
case SATURATION:
return new ColorHSL(this.getHue(), newValue, this.getLuminance());
case LUMINANCE:
return new ColorHSL(this.getHue(), this.getSaturation(), newValue);
}
return null;
}
public void dispose() {
color.dispose();
}
public Color getColor() {
return color;
}
public float getHue() {
return hue;
}
public float getSaturation() {
return saturation;
}
public float getLuminance() {
return luminance;
}
public float getPerceivedLuminance() {
return (299f * color.getRed() + 587f * color.getGreen() + 114f * color.getBlue()) / 1000;
}
public ColorHSL setHue(float hue) {
hue = setFloatBounds(hue, 0, 360);
return new ColorHSL(hue, this.getSaturation(), this.getLuminance());
}
public ColorHSL setSaturation(float saturation) {
saturation = setFloatBounds(saturation, 0, 1);
return new ColorHSL(this.getHue(), saturation, this.getLuminance());
}
public ColorHSL setLuminance(float luminance) {
luminance = setFloatBounds(luminance, 0, 1);
return new ColorHSL(this.getHue(), this.getSaturation(), luminance);
}
private static float setFloatBounds(float value, float min, float max) {
value = Math.min(value, max);
value = Math.max(value, min);
return value;
}
/*
* How the modification amount to a property should behave when hitting an upper
* or lower bound.
*/
enum BOUND_BEHAVIOR {
/*
* Jump from one bound to another. Example: For a color with a luminance of
* 0.6f, increasing the luminance by 0.5f while using the CYCLE BOUND_BEHAVIOR
* will result in the color having a luminance of 0.1f.
*/
CYCLE,
/*
* Negate any excess amount from the bound. Example: For a color with a
* luminance of 0.6f, increasing the luminance by 0.5f while using the REVERSE
* BOUND_BEHAVIOR will result in the color having a luminance of 0.9f.
*/
REVERSE,
/*
* Set the amount to the nearest bound. Example: Fora color with a luminance of
* 0.6f, increasing the luminance by 0.5f while using the LIMIT BOUND_BEHAVIOR
* will result in the color having a luminance of 1f.
*/
LIMIT;
}
enum HSL_PROPERTY {
HUE, SATURATION, LUMINANCE;
}
}