forked from daizhenjun/ImageFilterForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHslColor.java
More file actions
158 lines (138 loc) · 4.19 KB
/
Copy pathHslColor.java
File metadata and controls
158 lines (138 loc) · 4.19 KB
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
157
158
/*
* HaoRan ImageFilter Classes v0.4
* Copyright (C) 2012 Zhenjun Dai
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation.
*/
package HaoRan.ImageFilter;
public class HslColor
{
public float h;
public float l;
public float s;
public HslColor(float h, float s, float l)
{
this.h = h;
this.s = s;
this.l = l;
}
// HSL to RGB helper routine
private static double Hue_2_RGB(double v1, double v2, double vH)
{
if (vH < 0)
vH += 1;
if (vH > 1)
vH -= 1;
if ((6 * vH) < 1)
return (v1 + (v2 - v1) * 6 * vH);
if ((2 * vH) < 1)
return v2;
if ((3 * vH) < 2)
return (v1 + (v2 - v1) * ((2.0 / 3) - vH) * 6);
return v1;
}
public static int HslToRgb(HslColor hsl)
{
int r, g, b;
if (hsl.h == 0)
{
// gray values
r = g = b = (byte)(hsl.l * 255);
}
else
{
double v1, v2;
double hue = (double)hsl.h / 360;
v2 = (hsl.l < 0.5) ?
(hsl.l * (1 + hsl.s)) :
((hsl.l + hsl.s) - (hsl.l * hsl.s));
v1 = 2 * hsl.l - v2;
r = (byte)(255 * Hue_2_RGB(v1, v2, hue + (1.0 / 3)));
g = (byte)(255 * Hue_2_RGB(v1, v2, hue));
b = (byte)(255 * Hue_2_RGB(v1, v2, hue - (1.0 / 3)));
}
return (255 << 24) + (r << 16) + (g << 8) + b;
}
private static float HueToRgb(float t1, float t2, float h)
{
if (h < 0f)
{
h++;
}
if (h > 1f)
{
h--;
}
if ((6f * h) < 1f)
{
return (t1 + (((t2 - t1) * 6f) * h));
}
if ((2f * h) < 1f)
{
return t2;
}
if ((3f * h) < 2f)
{
return (t1 + (((t2 - t1) * (0.6666667f - h)) * 6f));
}
return t1;
}
public HslColor Interpolate(HslColor c2, float amount)
{
return new HslColor(this.h + ((c2.h - this.h) * amount), this.s + ((c2.s - this.s) * amount), this.l + ((c2.l - this.l) * amount));
}
public static void RgbToHsl(int color, HslColor hsl)
{
RgbToHsl(0xff & (color >> 0x10), 0xff & (color >> 8), 0xff & color, hsl);
}
public static void RgbToHsl(int rr, int gg, int bb, HslColor hsl)
{
double r = (rr / 255.0);
double g = (gg / 255.0);
double b = (bb / 255.0);
double min = Math.min(Math.min(r, g), b);
double max = Math.max(Math.max(r, g), b);
double delta = max - min;
// get luminance value
hsl.l = (float)(max + min) / 2;
if (delta == 0)
{
// gray color
hsl.h = 0;
hsl.s = 0.0f;
}
else
{
// get saturation value
hsl.s = (float)((hsl.l < 0.5) ? (delta / (max + min)) : (delta / (2 - max - min)));
// get hue value
double del_r = (((max - r) / 6) + (delta / 2)) / delta;
double del_g = (((max - g) / 6) + (delta / 2)) / delta;
double del_b = (((max - b) / 6) + (delta / 2)) / delta;
double hue;
if (r == max)
hue = del_b - del_g;
else if (g == max)
hue = (1.0 / 3) + del_r - del_b;
else
hue = (2.0 / 3) + del_g - del_r;
// correct hue if needed
if (hue < 0)
hue += 1;
if (hue > 1)
hue -= 1;
hsl.h = (int)(hue * 360);
}
}
}