Skip to content

Commit 14813e5

Browse files
authored
replace hsl color handling with hsv (#355)
Scratch 2 color and brightness effects in HSV color space.
1 parent 5bcbc54 commit 14813e5

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/shaders/sprite.frag

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ varying vec2 v_texCoord;
5454
// Smaller values can cause problems with "color" and "brightness" effects on some mobile devices
5555
const float epsilon = 1e-3;
5656

57-
// Convert an RGB color to Hue, Saturation, and Lightness.
57+
// Convert an RGB color to Hue, Saturation, and Value.
5858
// All components of input and output are expected to be in the [0,1] range.
59-
vec3 convertRGB2HSL(vec3 rgb)
59+
vec3 convertRGB2HSV(vec3 rgb)
6060
{
6161
// Hue calculation has 3 cases, depending on which RGB component is largest, and one of those cases involves a "mod"
6262
// operation. In order to avoid that "mod" we split the M==R case in two: one for G<B and one for B>G. The B>G case
@@ -80,13 +80,13 @@ vec3 convertRGB2HSL(vec3 rgb)
8080
// Chroma = M - m
8181
float C = temp2.x - m;
8282

83-
// Lightness = 1/2 * (M + m)
84-
float L = 0.5 * (temp2.x + m);
83+
// Value = M
84+
float V = temp2.x;
8585

8686
return vec3(
8787
abs(temp2.z + (temp2.w - temp2.y) / (6.0 * C + epsilon)), // Hue
88-
C / (1.0 - abs(2.0 * L - 1.0) + epsilon), // Saturation
89-
L); // Lightness
88+
C / (temp2.x + epsilon), // Saturation
89+
V); // Value
9090
}
9191

9292
vec3 convertHue2RGB(float hue)
@@ -97,11 +97,11 @@ vec3 convertHue2RGB(float hue)
9797
return clamp(vec3(r, g, b), 0.0, 1.0);
9898
}
9999

100-
vec3 convertHSL2RGB(vec3 hsl)
100+
vec3 convertHSV2RGB(vec3 hsv)
101101
{
102-
vec3 rgb = convertHue2RGB(hsl.x);
103-
float c = (1.0 - abs(2.0 * hsl.z - 1.0)) * hsl.y;
104-
return (rgb - 0.5) * c + hsl.z;
102+
vec3 rgb = convertHue2RGB(hsv.x);
103+
float c = hsv.z * hsv.y;
104+
return rgb * c + hsv.z - c;
105105
}
106106
#endif // !defined(DRAW_MODE_silhouette) && (defined(ENABLE_color) || defined(ENABLE_brightness))
107107

@@ -166,27 +166,27 @@ void main()
166166

167167
#if defined(ENABLE_color) || defined(ENABLE_brightness)
168168
{
169-
vec3 hsl = convertRGB2HSL(gl_FragColor.xyz);
169+
vec3 hsv = convertRGB2HSV(gl_FragColor.xyz);
170170

171171
#ifdef ENABLE_color
172172
{
173173
// this code forces grayscale values to be slightly saturated
174174
// so that some slight change of hue will be visible
175175
const float minLightness = 0.11 / 2.0;
176176
const float minSaturation = 0.09;
177-
if (hsl.z < minLightness) hsl = vec3(0.0, 1.0, minLightness);
178-
else if (hsl.y < minSaturation) hsl = vec3(0.0, minSaturation, hsl.z);
177+
if (hsv.z < minLightness) hsv = vec3(0.0, 1.0, minLightness);
178+
else if (hsv.y < minSaturation) hsv = vec3(0.0, minSaturation, hsv.z);
179179

180-
hsl.x = mod(hsl.x + u_color, 1.0);
181-
if (hsl.x < 0.0) hsl.x += 1.0;
180+
hsv.x = mod(hsv.x + u_color, 1.0);
181+
if (hsv.x < 0.0) hsv.x += 1.0;
182182
}
183183
#endif // ENABLE_color
184184

185185
#ifdef ENABLE_brightness
186-
hsl.z = clamp(hsl.z + u_brightness, 0.0, 1.0);
186+
hsv.z = clamp(hsv.z + u_brightness, 0.0, 1.0);
187187
#endif // ENABLE_brightness
188188

189-
gl_FragColor.rgb = convertHSL2RGB(hsl);
189+
gl_FragColor.rgb = convertHSV2RGB(hsv);
190190
}
191191
#endif // defined(ENABLE_color) || defined(ENABLE_brightness)
192192

0 commit comments

Comments
 (0)