{{newin|[[0.9.1]]|091|type=function}}
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 [http://http.developer.nvidia.com/GPUGems3/gpugems3_ch24.html here], [http://filmicgames.com/archives/299 here], and [http://renderwonk.com/blog/index.php/archive/adventures-with-gamma-correct-rendering/ here].

In versions prior to [[11.0]], color component values were within the range of 0 to 255 instead of 0 to 1.
{{notice|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 ===
<source lang="lua">
cr, cg, cb = love.math.linearToGamma( lr, lg, lb )
</source>
=== Arguments ===
{{param|number|lr|The red channel of the linear RGB color to convert.}}
{{param|number|lg|The green channel of the linear RGB color to convert.}}
{{param|number|lb|The blue channel of the linear RGB color to convert.}}
=== Returns ===
{{param|number|cr|The red channel of the converted color in gamma sRGB space.}}
{{param|number|cg|The green channel of the converted color in gamma sRGB space.}}
{{param|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 ===
<source lang="lua">
cr, cg, cb = love.math.linearToGamma( color )
</source>
=== Arguments ===
{{param|table|color|An array with the red, green, and blue channels of the linear RGB color to convert.}}
=== Returns ===
{{param|number|cr|The red channel of the converted color in gamma sRGB space.}}
{{param|number|cg|The green channel of the converted color in gamma sRGB space.}}
{{param|number|cb|The blue channel of the converted color in gamma sRGB space.}}

== Function ==
=== Synopsis ===
<source lang="lua">
c = love.math.linearToGamma( lc )
</source>
=== Arguments ===
{{param|number|lc|The value of a color channel in linear RGB space to convert.}}
=== Returns ===
{{param|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 ===
<source lang="lua">
local function PremultiplyLinearPixel(x, y, r, g, b, a)
   r = r * a
   g = g * a
   b = b * a
   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
   g = g * a
   b = b * a
   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 ('alpha', 'premultiplied') blend mode.
-- The multiplication correctly accounts for the color-space of the image.
function NewPremultipliedImage(filepath, flags)
    local imagedata = love.image.newImageData(filepath)

    local mapfunction = (flags and flags.linear) and PremultiplyLinearPixel or PremultiplyGammaPixel
    imagedata:mapPixel(mapfunction)

    return love.graphics.newImage(imagedata, flags)
end

image = NewPremultipliedImage("pig.png")
</source>

== See Also ==
* [[parent::love.math]]
* [[love.math.gammaToLinear]]
[[Category:Functions]]
{{#set:Description=Converts a color from linear-space (RGB) to gamma-space (sRGB).}}
== Other Languages ==
{{i18n|love.math.linearToGamma}}