Compiler1 min read

Color Module

Color-space conversion and tone-mapping helpers.

Reading Time
1 min
Word Count
101
Sections
9
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);

Constants

None.

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

Source

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