Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions jme3-core/src/main/resources/Common/ShaderLib/HSVUtils.glsllib
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Functions for converting between RGB and HSV to allow for adjusting colors in HSV color space

vec3 rgb2hsv(vec3 c){
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The magic number 1.0e-10 is used to prevent division by zero. It's better to define it as a named constant to improve readability and explain its purpose. This also makes it easier to change if needed.

    const float EPSILON = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + EPSILON)), d / (q.x + EPSILON), q.x);

}

vec3 hsv2rgb(vec3 c){
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

vec3 alterColorWithHsvOffset(vec3 color, vec3 hsvOffset){
vec3 colorHSV = rgb2hsv(color);
colorHSV += hsvOffset;

// wrap hue
colorHSV.x = fract(colorHSV.x);

// clamp saturation and value
colorHSV.yz = clamp(colorHSV.yz, 0.0, 1.0);

return hsv2rgb(colorHSV);
}
Loading