[−][src]Module prisma::encoding
Traits and structs for dealing with and changing a color's encoding
Background:
Most colors used on a computer are encoded using a gamma encoding scheme. This serves two important purposes:
-
Gamma encoding with $
\gamma \approx 2.2
$ produces an approximately linear lightness increase (that is, a gradient from black to white should appear to be smooth and consistent throughout). This is because the human visual system is logarithmic, Doubling the light into your eye does not make something appear twice as bright. -
Because of the above, gamma encoding also is an important compression tool. Without gamma encoding, there would be far more light shades than dark shades when using 8-bit channels, and the brightest shades would be virtually indistinguishable. With a proper gamma encoding, each shade is approximately equidistant from each other, and thus each of the 256 values in an 8-bit channel are equally important and carry equal amounts of information.
It is a common misconception that gamma encoding is a relic from the non-uniform response of CRT monitors. Gamma encodings were used to correct that, but that is not why they are used today, nor are they a relic to be forgotten. Linear 8-bit RGB is still an unsuitable format for display.
For floating point channels, a lack of gamma encoding will make them values perceptually non-linear, but there is enough information stored that the compression is not important.
Encoding:
Encoding Schemes:
Prisma provides three different encoding schemes:
LinearEncoding
A color with no encoding at all, linear in intensitySrgbEncoding
A modified gamma encoding used specifically with the sRGB color spaceGammaEncoding
A general gamma encoding with specified value for gamma
A color can have its encoding specified in the type system by wrapping it in EncodedColor
.
Details:
Encoding and decoding colors is primarily done through EncodedColor
, though a lower level interface
exists via the TranscodableColor
trait. Only Rgb
and Rgba
colors
can have their encoding changed. This is due to the fact that gamma encodings are non-linear, and
doing math to convert between models will not preserve the same decoding method. Thus, in order
to change encodings of other device-dependent color models, you must first convert to Rgb, then
change the encoding, then convert back.
A EncodedColor
object can be produced from a color with a known encoding using the
encoded_as
method of DeviceDependentColor
. This does not
do any conversion, it's up to you to ensure the color is actually encoded as you specify.
Examples:
Converting from sRGB encoding to linear and back:
use prisma::encoding::{TranscodableColor, EncodableColor, SrgbEncoding}; use prisma::Rgb; let srgb_color = Rgb::new(200, 200, 200u8).srgb_encoded(); let linear_color = srgb_color.decode(); // Do some transformation let srgb_color = linear_color.encode(SrgbEncoding::new());
Structs
EncodedColor | A color decorated with its encoding. This is the primary way to use encodings. |
GammaEncoding | A gamma encoding scheme with a given value for $ |
LinearEncoding | A linear encoding scheme |
SrgbEncoding | An encoding scheme used by the sRGB color space. |
Traits
ChannelDecoder | An object that can decode a color from some encoding to a linear encoding |
ChannelEncoder | An object that can encode a color from a linear encoding to a different encoding |
ColorEncoding | An object able to encode and decode a color |
EncodableColor | A color that can be stored in an |
TranscodableColor | A color that can have its encoding changed |
Type Definitions
LinearColor | A color with a linear encoding |