Difference between revisions of "love.math.colorFromBytes"
(→Notes) |
(→Example) |
||
(One intermediate revision by the same user not shown) | |||
Line 44: | Line 44: | ||
local function setColorHEX(rgba) | local function setColorHEX(rgba) | ||
-- setColorHEX(rgba) | -- setColorHEX(rgba) | ||
− | -- where rgba is string as "# | + | -- where rgba is string as "#336699cc" |
local rb = tonumber(string.sub(rgba, 2, 3), 16) | local rb = tonumber(string.sub(rgba, 2, 3), 16) | ||
local gb = tonumber(string.sub(rgba, 4, 5), 16) | local gb = tonumber(string.sub(rgba, 4, 5), 16) | ||
local bb = tonumber(string.sub(rgba, 6, 7), 16) | local bb = tonumber(string.sub(rgba, 6, 7), 16) | ||
local ab = tonumber(string.sub(rgba, 8, 9), 16) or nil | local ab = tonumber(string.sub(rgba, 8, 9), 16) or nil | ||
− | -- print (rb, gb, bb, ab) -- prints | + | -- print (rb, gb, bb, ab) -- prints 51 102 153 204 |
+ | -- print (love.math.colorFromBytes( rb, gb, bb, ab )) -- prints 0.2 0.4 0.6 0.8 | ||
love.graphics.setColor (love.math.colorFromBytes( rb, gb, bb, ab )) | love.graphics.setColor (love.math.colorFromBytes( rb, gb, bb, ab )) | ||
end | end |
Latest revision as of 08:44, 13 October 2022
Available since LÖVE 11.3 |
This function is not supported in earlier versions. |
Converts a color from 0..255 to 0..1 range.
Contents
Function
Synopsis
r, g, b, a = love.math.colorFromBytes( rb, gb, bb, ab )
Arguments
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.
Returns
number r
- Red color component in 0..1 range.
number g
- Green color component in 0..1 range.
number b
- Blue color component in 0..1 range.
number a (nil)
- Alpha color component in 0..1 range or nil if alpha is not specified.
Notes
Here's implementation for 11.2 and earlier.
function love.math.colorFromBytes(r, g, b, a)
if type(r) == "table" then
r, g, b, a = r[1], r[2], r[3], r[4]
end
r = clamp01(floor(r + 0.5) / 255)
g = clamp01(floor(g + 0.5) / 255)
b = clamp01(floor(b + 0.5) / 255)
a = a ~= nil and clamp01(floor(a + 0.5) / 255) 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
Example
local function setColorHEX(rgba)
-- setColorHEX(rgba)
-- where rgba is string as "#336699cc"
local rb = tonumber(string.sub(rgba, 2, 3), 16)
local gb = tonumber(string.sub(rgba, 4, 5), 16)
local bb = tonumber(string.sub(rgba, 6, 7), 16)
local ab = tonumber(string.sub(rgba, 8, 9), 16) or nil
-- print (rb, gb, bb, ab) -- prints 51 102 153 204
-- print (love.math.colorFromBytes( rb, gb, bb, ab )) -- prints 0.2 0.4 0.6 0.8
love.graphics.setColor (love.math.colorFromBytes( rb, gb, bb, ab ))
end
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