Views
love.math.linearToGamma
Available since LÖVE 0.9.1 |
This function is not supported in earlier versions. |
Converts a color from linear-space (RGB) to gamma-space (sRGB). This is useful when storing linear RGB color values in an image, because the linear RGB color space has less precision than sRGB for dark colors, which can result in noticeable color banding when drawing.
In general, colors chosen based on what they look like on-screen are already in gamma-space and should not be double-converted. Colors calculated using math are often in the linear RGB space.
Read more about gamma-correct rendering here, here, and here.
Gamma-correct rendering is an advanced topic and it's easy to get color-spaces mixed up. If you're not sure whether you need this, you might want to avoid it. |
Function
Synopsis
Arguments
number lr
- The red channel of the linear RGB color to convert.
number lg
- The green channel of the linear RGB color to convert.
number lb
- The blue channel of the linear RGB color to convert.
Returns
number cr
- The red channel of the converted color in gamma sRGB space.
number cg
- The green channel of the converted color in gamma sRGB space.
number cb
- The blue channel of the converted color in gamma sRGB space.
Notes
An alpha value can be passed into the function as a fourth argument, but it will be returned unchanged because alpha is always linear.
Function
Synopsis
Arguments
table color
- An array with the red, green, and blue channels of the linear RGB color to convert.
Returns
number cr
- The red channel of the converted color in gamma sRGB space.
number cg
- The green channel of the converted color in gamma sRGB space.
number cb
- The blue channel of the converted color in gamma sRGB space.
Function
Synopsis
Arguments
number lc
- The value of a color channel in linear RGB space to convert.
Returns
number c
- The value of the color channel in gamma sRGB space.
Examples
Pre-multiply an image's alpha with its RGB values in linear RGB space
r = r * a / 255
g = g * a / 255
b = b * a / 255
return r, g, b, a
end
local function PremultiplyGammaPixel(x, y, r, g, b, a)
r, g, b = love.math.gammaToLinear(r, g, b)
r = r * a / 255
g = g * a / 255
b = b * a / 255
r, g, b = love.math.linearToGamma(r, g, b)
return r, g, b, a
end
-- Loads an image and pre-multiplies its RGB values with its alpha, for use with the 'premultiplied' alpha blend mode.
-- The multiplication correctly accounts for the color-space of the image.
function NewPremultipliedImage(filepath, format)
local imagedata = love.image.newImageData(filepath)
local mapfunction = format == "srgb" and PremultiplyGammaPixel or PremultiplyLinearPixel
imagedata:mapPixel(mapfunction)
return love.graphics.newImage(imagedata, format)
end
-- This assumes gamma-correct rendering (with the sRGB window flag) is being done.
image = NewPremultipliedImage("pig.png", "srgb")
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info