Compiler2 min read

Color Module

Color-space conversion and tone-mapping helpers.

Reading Time
2 min
Word Count
221
Sections
10
Try It Live

Turn the guide into code

Take the key idea from this page into the playground and validate it in a real shader instead of leaving it as theory.

Open Playground

Color-space conversion and tone-mapping helpers.

Import

bwsl
import Color

This module imports: PostFX.

Usage Examples

Decode Texture Color Before Lighting

Use Color when material textures or UI-picked colors are authored in sRGB but lighting math needs linear values.

bwsl
import Color
float4 texel = sample(resources.albedoTex, resources.albedoSampler, input.uv);
float3 albedoLinear = Color::sRGBToLinear(texel.rgb);
float3 N = normalize(input.normal);
float3 L = normalize(resources.lightDir);
float lit = max(dot(N, L), 0.0);
float3 linearColor = albedoLinear * (resources.ambient + lit * resources.lightColor);
output.color = float4(Color::linearToSRGB(linearColor), texel.a);

Apply Tonemapping at the Display Boundary

Use Color tone mapping helpers when a shader produces HDR lighting but the final pass must write displayable color.

bwsl
import Color
float3 hdr = resources.emissiveColor * resources.emissiveStrength;
hdr += resources.diffuseLighting;
hdr += resources.specularLighting;
float3 mapped = Color::ACESFilm(hdr * resources.exposure);
float3 display = Color::linearToSRGB(mapped);
output.color = float4(display, 1.0);

Perceptual Hue Ramps

Use the HSLuv conversions when a gradient should change hue without the brightness jumps that HSV ramps produce.

bwsl
import Color
// HSLuv: hue in degrees, saturation and lightness in [0, 100].
float hue = input.uv.x * 360.0;
float3 rgb = Color::hsluvToRgb(float3(hue, 85.0, 65.0));
output.color = float4(rgb, 1.0);

Constants

const float HSLUV_EPSILON = 1e-5const float HSLUV_CHROMA_EPSILON = 1e-4

Structs

No structs found.

Enums

No enums found.

Functions

sRGBToLinear

sRGB to linear

bwsl
sRGBToLinear :: (float3 srgb) -> float3

linearToSRGB

Linear to sRGB

bwsl
linearToSRGB :: (float3 linear) -> float3

ACESFilm

ACES filmic tone mapping

bwsl
ACESFilm :: (float3 x) -> float3

reinhardToneMap

Reinhard tone mapping

bwsl
reinhardToneMap :: (float3 hdr) -> float3

reinhardExtendedToneMap

Extended Reinhard with white point

bwsl
reinhardExtendedToneMap :: (float3 hdr, float whitePoint) -> float3

xyzToRgb

bwsl
xyzToRgb :: (float3 tuple) -> float3

rgbToXyz

bwsl
rgbToXyz :: (float3 tuple) -> float3

xyzToLuv

bwsl
xyzToLuv :: (float3 tuple) -> float3

luvToXyz

bwsl
luvToXyz :: (float3 tuple) -> float3

luvToLch

bwsl
luvToLch :: (float3 tuple) -> float3

lchToLuv

bwsl
lchToLuv :: (float3 tuple) -> float3

hsluvToLch

bwsl
hsluvToLch :: (float3 tuple) -> float3

lchToHsluv

bwsl
lchToHsluv :: (float3 tuple) -> float3

lchToRgb

bwsl
lchToRgb :: (float3 tuple) -> float3

rgbToLch

bwsl
rgbToLch :: (float3 tuple) -> float3

hsluvToRgb

bwsl
hsluvToRgb :: (float3 tuple) -> float3

rgbToHsluv

bwsl
rgbToHsluv :: (float3 tuple) -> float3

hpluvToLch

bwsl
hpluvToLch :: (float3 tuple) -> float3

hpluvToRgb

bwsl
hpluvToRgb :: (float3 tuple) -> float3

lchToHpluv

bwsl
lchToHpluv :: (float3 tuple) -> float3

rgbToHpluv

bwsl
rgbToHpluv :: (float3 tuple) -> float3

hsv2rgb

bwsl
hsv2rgb :: (float3 c) -> float3

rgb2hsv

bwsl
rgb2hsv :: (float3 c) -> float3

hsluv_maxLForH

Calculates the maximum HSLUV lightness for a given hue, so that one component in the RGB model would be 0. Once this lightness is exceeded, the color will become more white. This is useful for custom shading of "shining" objects, where the brightest parts are white. such as neon lights and lasers. Examples: blue L=75.6 #b3b3ff -> L=32.3 #0000ff orange L=73.1 #ff9974 -> L=64.7 #ff7400

bwsl
hsluv_maxLForH :: (float hue) -> float

Source

Generated from compiler-reference/modules/Color.bwsl.