Difference between revisions of "love.math.colorToBytes"

(Created page with "{{newin|11.3|113|type=function}} Converts a color from 0..1 to 0..255 range. == Function == === Synopsis === <source lang="lua"> rb, gb, bb, ab = love.math.colorToBytes(...")
 
m (Add clamp01 function)
 
Line 30: Line 30:
 
a = a ~= nil and floor(clamp01(a) * 255 + 0.5) or nil
 
a = a ~= nil and floor(clamp01(a) * 255 + 0.5) or nil
 
return r, g, b, a
 
return r, g, b, a
 +
end
 +
</source>
 +
Where <code>clamp01</code> is defined as follows
 +
<source lang="lua">
 +
local function clamp01(x)
 +
return math.min(math.max(x, 0), 1)
 
end
 
end
 
</source>
 
</source>

Latest revision as of 02:25, 20 December 2018

Available since LÖVE 11.3
This function is not supported in earlier versions.

Converts a color from 0..1 to 0..255 range.

Function

Synopsis

rb, gb, bb, ab = love.math.colorToBytes( r, g, b, a )

Arguments

number r
Red color component.
number g
Green color component.
number b
Blue color component.
number a (nil)
Alpha color component.

Returns

number rb
Red color component in 0..255 range.
number gb
Green color component in 0..255 range.
number bb
Blue color component in 0..255 range.
number ab (nil)
Alpha color component in 0..255 range or nil if alpha is not specified.

Notes

Here's implementation for 11.2 and earlier.

function love.math.colorToBytes(r, g, b, a)
	if type(r) == "table" then
		r, g, b, a = r[1], r[2], r[3], r[4]
	end
	r = floor(clamp01(r) * 255 + 0.5)
	g = floor(clamp01(g) * 255 + 0.5)
	b = floor(clamp01(b) * 255 + 0.5)
	a = a ~= nil and floor(clamp01(a) * 255 + 0.5) or nil
	return r, g, b, a
end

Where clamp01 is defined as follows

local function clamp01(x)
	return math.min(math.max(x, 0), 1)
end

See Also


Other Languages