silk.color.srgb.
The sRGB transfer function: conversion between gamma-encoded sRGB channels and linear light.
When to use#
Use toLinear before blending, filtering, or averaging sRGB values in linear light, and
fromLinear to encode the result again. Use toLinearRgba and fromLinearRgba to
convert the three color channels of a silk.color.rgba.Rgba<f64> at once, leaving alpha
unchanged.
Details#
The functions implement the piecewise curve of IEC 61966-2-1 with the constants that CSS Color 4
uses: a linear segment below 0.04045 (encoded) or 0.0031308 (linear), and a 2.4 power curve
above it. Negative inputs mirror the curve through zero, so values outside [0, 1] from
wide-gamut conversions round-trip instead of becoming NaN. The toLinearF32 and
fromLinearF32 forms compute in f64 and round once.
The same curve also encodes Display P3 channels, because that space shares the sRGB transfer function.
Examples#
Average two encoded values in linear light#
import silk.color.srgb
import silk.num.f64
pub fn main() -> i32 {
let black = srgb.toLinear(0.0)
let white = srgb.toLinear(1.0)
let middle = srgb.fromLinear((black + white) / 2.0)
if f64.abs(middle - 0.7353569830524495) > 1.0e-12 {
return 1
}
return 0
}See also#
Use silk.color.color.Color for conversions between whole color spaces, such as sRGB to Oklch.
Import as srgb with import silk.color.srgb as srgb.
Public declarations: 6.
toLinear#
pub fn toLinear(value: f64) -> f64Returns the linear-light value of the gamma-encoded sRGB channel value.
Details#
0 maps to 0 and 1 maps to 1. Below 0.04045 the curve is value / 12.92; above it,
((value + 0.055) / 1.055) ^ 2.4. A negative value returns the negated result for its
magnitude, so out-of-gamut channels keep their sign.
fromLinear#
pub fn fromLinear(value: f64) -> f64Returns the gamma-encoded sRGB channel for the linear-light value value.
Details#
This is the inverse of toLinear. Below 0.0031308 the curve is value * 12.92; above it,
1.055 * value ^ (1 / 2.4) - 0.055. A negative value returns the negated result for its
magnitude.
toLinearF32#
pub fn toLinearF32(value: f32) -> f32Returns the linear-light value of the gamma-encoded sRGB channel value, as f32.
Details#
The curve is computed in f64 and rounded once to f32. 0 maps to 0, 1 maps to 1, and
a negative value returns the negated result for its magnitude.
fromLinearF32#
pub fn fromLinearF32(value: f32) -> f32Returns the gamma-encoded sRGB channel for the linear-light value value, as f32.
Details#
The curve is computed in f64 and rounded once to f32. A negative value returns the negated
result for its magnitude.
toLinearRgba#
pub fn toLinearRgba(color: Rgba<f64>) -> Rgba<f64>Returns color with its red, green, and blue channels decoded from sRGB to linear light.
Details#
Alpha is copied unchanged. The input must have straight (not premultiplied) alpha, because the curve is not linear: decode first, then premultiply.
fromLinearRgba#
pub fn fromLinearRgba(color: Rgba<f64>) -> Rgba<f64>Returns color with its red, green, and blue channels encoded from linear light to sRGB.
Details#
Alpha is copied unchanged. The input must have straight (not premultiplied) alpha: unpremultiply first, then encode.
Category: Color.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.