hslToRgb

HSL (Hue, Saturation, Lightness) color to RGB.

Parameters

NameTypeDescription
hslvec3<f32>HSL color values (hue: 0-1, saturation: 0-1, lightness: 0-1).

Returns

vec3<f32>color values.

Dependencies

WGSL Code

//! requires hueToRgb
fn hslToRgb(hsl: vec3<f32>) -> vec3<f32> {
let h = hsl.x;
let s = hsl.y;
let l = hsl.z;
if (s == 0.0) {
return vec3(l); // achromatic
}
let q = select(l * (1.0 + s), l + s - l * s, l < 0.5);
let p = 2.0 * l - q;
return vec3(
hueToRgb(p, q, h + 1.0 / 3.0),
hueToRgb(p, q, h),
hueToRgb(p, q, h - 1.0 / 3.0)
);
}
Showing 5/19 lines

hueToRgb

function for HSL to RGB conversion - converts hue component to RGB.

Parameters

NameTypeDescription
pf32First HSL conversion parameter.
qf32Second HSL conversion parameter.
tf32Hue value (adjusted).

Returns

f32component value.

WGSL Code

fn hueToRgb(p: f32, q: f32, t: f32) -> f32 {
var t_adj = t;
if (t_adj < 0.0) { t_adj += 1.0; }
if (t_adj > 1.0) { t_adj -= 1.0; }
if (t_adj < 1.0 / 6.0) { return p + (q - p) * 6.0 * t_adj; }
if (t_adj < 1.0 / 2.0) { return q; }
if (t_adj < 2.0 / 3.0) { return p + (q - p) * (2.0 / 3.0 - t_adj) * 6.0; }
return p;
}
Showing 5/9 lines

linearToSrgb

linear RGB color values to sRGB color space.

Parameters

NameTypeDescription
colorvec3<f32>Linear RGB color values.

Returns

vec3<f32>color values.

WGSL Code

fn linearToSrgb(color: vec3<f32>) -> vec3<f32> {
return pow(color, vec3(1.0 / 2.2));
}

palette

colors using cosine-based palette function for smooth color gradients.

Parameters

NameTypeDescription
tf32Input parameter (typically 0-1) for palette lookup.
avec3<f32>Offset values for RGB channels.
bvec3<f32>Amplitude values for RGB channels.
cvec3<f32>Frequency values for RGB channels.
dvec3<f32>Phase values for RGB channels.

Returns

vec3<f32>RGB color.

WGSL Code

fn palette(t: f32, a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, d: vec3<f32>) -> vec3<f32> {
return a + b * cos(6.28318 * (c * t + d));
}

srgbToLinear

sRGB color values to linear RGB color space.

Parameters

NameTypeDescription
colorvec3<f32>sRGB color values.

Returns

vec3<f32>RGB color values.

WGSL Code

fn srgbToLinear(color: vec3<f32>) -> vec3<f32> {
return pow(color, vec3(2.2));
}