Color Module
Color-space conversion and tone-mapping helpers.
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 PlaygroundColor-space conversion and tone-mapping helpers.
Import
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.
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.
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.
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-4Structs
No structs found.
Enums
No enums found.
Functions
sRGBToLinear
sRGB to linear
sRGBToLinear :: (float3 srgb) -> float3
linearToSRGB
Linear to sRGB
linearToSRGB :: (float3 linear) -> float3
ACESFilm
ACES filmic tone mapping
ACESFilm :: (float3 x) -> float3
reinhardToneMap
Reinhard tone mapping
reinhardToneMap :: (float3 hdr) -> float3
reinhardExtendedToneMap
Extended Reinhard with white point
reinhardExtendedToneMap :: (float3 hdr, float whitePoint) -> float3
xyzToRgb
xyzToRgb :: (float3 tuple) -> float3
rgbToXyz
rgbToXyz :: (float3 tuple) -> float3
xyzToLuv
xyzToLuv :: (float3 tuple) -> float3
luvToXyz
luvToXyz :: (float3 tuple) -> float3
luvToLch
luvToLch :: (float3 tuple) -> float3
lchToLuv
lchToLuv :: (float3 tuple) -> float3
hsluvToLch
hsluvToLch :: (float3 tuple) -> float3
lchToHsluv
lchToHsluv :: (float3 tuple) -> float3
lchToRgb
lchToRgb :: (float3 tuple) -> float3
rgbToLch
rgbToLch :: (float3 tuple) -> float3
hsluvToRgb
hsluvToRgb :: (float3 tuple) -> float3
rgbToHsluv
rgbToHsluv :: (float3 tuple) -> float3
hpluvToLch
hpluvToLch :: (float3 tuple) -> float3
hpluvToRgb
hpluvToRgb :: (float3 tuple) -> float3
lchToHpluv
lchToHpluv :: (float3 tuple) -> float3
rgbToHpluv
rgbToHpluv :: (float3 tuple) -> float3
hsv2rgb
hsv2rgb :: (float3 c) -> float3
rgb2hsv
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
hsluv_maxLForH :: (float hue) -> float
Source
Generated from compiler-reference/modules/Color.bwsl.