m
Move page script moved page USERAPI HexToRGBPerc to WoW:USERAPI HexToRGBPerc without leaving a redirect
No edit summary |
m (Move page script moved page USERAPI HexToRGBPerc to WoW:USERAPI HexToRGBPerc without leaving a redirect) |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
{{userfunc}} | {{userfunc}} | ||
__NOTOC__ | __NOTOC__ | ||
Takes a hex string (must be 6 char long!) and converts it to a RGB percent set (0.0-1.0) | Takes a hex string (must be 6 char long!) and converts it to a RGB percent set (0.0-1.0) | ||
| Line 7: | Line 8: | ||
local rhex, ghex, bhex = string.sub(hex, 1, 2), string.sub(hex, 3, 4), string.sub(hex, 5, 6) | local rhex, ghex, bhex = string.sub(hex, 1, 2), string.sub(hex, 3, 4), string.sub(hex, 5, 6) | ||
return tonumber(rhex, 16)/255, tonumber(ghex, 16)/255, tonumber(bhex, 16)/255 | return tonumber(rhex, 16)/255, tonumber(ghex, 16)/255, tonumber(bhex, 16)/255 | ||
end | |||
== Alternative Version == | |||
Takes a hex string (must be 3 or 6 chars long!) and converts it to a RGB percent set (0.0-1.0). If ''hex'' is invalid, return black (0,0,0). | |||
local function HexToRGBPerc(hex) | |||
local rhex, ghex, bhex, base | |||
if strlen(hex) == 6 then | |||
rhex, ghex, bhex = strmatch('([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})', hex) | |||
base = 255 | |||
elseif strlen(hex) == 3 then | |||
rhex, ghex, bhex = strmatch('([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])', hex) | |||
base = 16 | |||
end | |||
if not (rhex and ghex and bhex) then | |||
return 0, 0, 0 | |||
else | |||
return tonumber(rhex, 16)/base, tonumber(ghex, 16)/base, tonumber(bhex, 16)/base | |||
end | |||
end | end | ||