Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1#define_import_path bevy_render::color_operations
3#import bevy_render::maths::FRAC_PI_3
5// Converts HSV to RGB.
6//
7// Input: H ∈ [0, 2π), S ∈ [0, 1], V ∈ [0, 1].
8// Output: R ∈ [0, 1], G ∈ [0, 1], B ∈ [0, 1].
9//
10// <https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative>
11fn hsv_to_rgb(hsv: vec3<f32>) -> vec3<f32> {
12 let n = vec3(5.0, 3.0, 1.0);
13 let k = (n + hsv.x / FRAC_PI_3) % 6.0;
14 return hsv.z - hsv.z * hsv.y * max(vec3(0.0), min(k, min(4.0 - k, vec3(1.0))));
17// Converts RGB to HSV.
18//
19// Input: R ∈ [0, 1], G ∈ [0, 1], B ∈ [0, 1].
20// Output: H ∈ [0, 2π), S ∈ [0, 1], V ∈ [0, 1].
21//
22// <https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB>
23fn rgb_to_hsv(rgb: vec3<f32>) -> vec3<f32> {
24 let x_max = max(rgb.r, max(rgb.g, rgb.b)); // i.e. V
25 let x_min = min(rgb.r, min(rgb.g, rgb.b));
26 let c = x_max - x_min; // chroma
28 var swizzle = vec3<f32>(0.0);
29 if (x_max == rgb.r) {
30 swizzle = vec3(rgb.gb, 0.0);
31 } else if (x_max == rgb.g) {
32 swizzle = vec3(rgb.br, 2.0);
33 } else {
34 swizzle = vec3(rgb.rg, 4.0);
35 }
37 let h = FRAC_PI_3 * (((swizzle.x - swizzle.y) / c + swizzle.z) % 6.0);
39 // Avoid division by zero.
40 var s = 0.0;
41 if (x_max > 0.0) {
42 s = c / x_max;
43 }
45 return vec3(h, s, x_max);
Contents