Documentation for this module may be created at Module:Color convert/doc
--
--
--
local p = {}
--
-- RGB to HSL functions
-- Converts rgb(x,y,z) to HSL
-- x, y, and z are all in the range [0,255]
-- H is in the range [0,360]
-- S and L are in the range [0,100]
--
function p.rgb_to_hsl(frame)
local red = tonumber(frame.args[1])
local green = tonumber(frame.args[2])
local blue = tonumber(frame.args[3])
return to_text(p._rgb_to_hsl(red,green,blue),'hsl')
end
function p._rgb_to_hsl(red,green,blue)
local hue
local sat
local lum
local r_prime = red/255
local g_prime = green/255
local b_prime = blue/255
local c_max = math.max(r_prime,g_prime,b_prime)
local c_min = math.min(r_prime,g_prime,b_prime)
local c_dif = c_max - c_min
-- Calculate hue
if c_max == r_prime then
hue = 60 * math.fmod((g_prime - b_prime)/c_dif,6)
elseif c_max == g_prime then
hue = 60 * (((b_prime - r_prime)/c_dif)+2)
elseif c_max == b_prime then
hue = 60 * (((r_prime - g_prime)/c_dif)+4)
end
-- Calculate lightness/luminosity
lum = (c_max + c_min)/2
-- Calculate saturation
-- Requires lum to be calculated
if c_dif == 0 then
sat = 0
else
sat = c_dif/(1 - math.abs(2 * lum - 1))
end
hue = math.fmod(math.floor(hue)+3600,360)
sat = math.floor(sat*1000)/10
lum = math.floor(lum*1000)/10
return {hue,sat,lum}
end
--
-- RGB to Jagex's HSL functions
-- Converts rgb(x,y,z) to jHSL
-- x, y, and z are all in the range [0,255]
-- H is in the range [0,63]
-- S is in the range [0,7]
-- L in in the range [0,127]
-- Piggy backs on the standard rgb to HSL functions
--
function p.rgb_to_jagex(frame)
local red = tonumber(frame.args[1])
local green = tonumber(frame.args[2])
local blue = tonumber(frame.args[3])
return to_text(p._rgb_to_jagex(red,green,blue),'jagex')
end
function p._rgb_to_jagex(red,green,blue)
local args = p._rgb_to_hsl(red,green,blue)
local hue = math.floor((args[1] / 360) * 65)
local sat = math.floor((args[2] / 100) * 7)
local lum = math.floor((args[3] / 100) * 170)
return {hue,sat,lum}
end
function p.jagex_to_rgb(frame)
local hue = tonumber(frame.args[1])
local sat = tonumber(frame.args[2])
local lum = tonumber(frame.args[3])
return to_text(p._jagex_to_rgb(hue,sat,lum),'rgb')
end
function p._jagex_to_rgb(hue,sat,lum)
hue = (hue/65)*360
sat = (sat/7)*100
lum = (lum/170)*100
return p._hsl_to_rgb(hue,sat,lum)
end
--
-- HSL to rgb funcions
-- Converts HSL to rgb(x,y,z)
-- x, y, and z are all in the range [0,255]
-- H is in the range [0,63]
-- S is in the range [0,7]
-- L in in the range [0,127]
--
function p.hsl_to_rgb(frame)
local hue = tonumber(frame.args[1])
local sat = tonumber(frame.args[2])
local lum = tonumber(frame.args[3])
return to_text(p._hsl_to_rgb(hue,sat,lum),'rgb')
end
function p._hsl_to_rgb(hue,sat,lum)
sat = sat/100
lum = lum/100
local a = sat * (1 - math.abs(2 * lum - 1))
local b = a * (1 - math.abs(math.fmod(hue / 60,2) - 1))
local c = lum - (a/2)
local ab_args
local form = {
[1] = {a,b,0}, -- 0 <= H < 60
[2] = {b,a,0}, -- 60 <= H < 120
[3] = {0,a,b}, -- 120 <= H < 180
[4] = {0,b,a}, -- 180 <= H < 240
[5] = {b,0,a}, -- 240 <= H < 300
[6] = {a,0,b} -- 300 >= H < 360
}
if hue >= 0 and hue < 60 then
ab_args = form[1]
elseif hue >= 60 and hue < 120 then
ab_args = form[2]
elseif hue >= 120 and hue < 180 then
ab_args = form[3]
elseif hue >= 180 and hue < 240 then
ab_args = form[4]
elseif hue >= 240 and hue < 300 then
ab_args = form[5]
elseif hue >= 300 then
ab_args = form[6]
end
local r = math.floor(255*(ab_args[1]+c))
local g = math.floor(255*(ab_args[2]+c))
local b = math.floor(255*(ab_args[3]+c))
return {r,g,b}
end
--
-- RGB to/from HEX functions
-- Converts HSL to rgb(x,y,z)
-- x, y, and z are all in the range [0,255]
-- HEX values are all in the range [0x00,0xFF]
--
function p.rgb_to_hex(frame)
local red = tonumber(frame.args[1])
local green = tonumber(frame.args[2])
local blue = tonumber(frame.args[3])
return to_text(p._rgb_to_hex(red,green,blue),'hex')
end
function p._rgb_to_hex(red,green,blue)
red = dec_to_hex(red)
green = dec_to_hex(green)
blue = dec_to_hex(blue)
return {red,green,blue}
end
function p.hex_to_rgb(frame)
local color = frame.args[1]
local red = string.sub(color,1,2)
local green = string.sub(color,3,4)
local blue = string.sub(color,5,6)
return to_text(p._hex_to_rgb(red,green,blue),'rgb')
end
function p._hex_to_rgb(red,green,blue)
red = hex_to_dec(red)
green = hex_to_dec(green)
blue = hex_to_dec(blue)
return {red,green,blue}
end
--
-- Base conversion
--
-- Short and nonspecific for efficiency
--
local nums = '0123456789ABCDEF'
function hex_to_dec(hex)
hex = hex:upper()
local d1 = nums:find(string.sub(hex,1,1),1,true) - 1
local d2 = nums:find(string.sub(hex,-1),1,true) - 1
local dec = d1 * 16 + d2
return dec
end
function dec_to_hex(dec)
local d1 = math.floor(dec/16) + 1
local d2 = math.fmod(dec,16) + 1
return string.sub(nums,d1,d1)..string.sub(nums,d2,d2)
end
--
-- HEX to and from HSL
-- Piggybacks on the HEX/RGB and RGB/HEX functions
--
function p.hex_to_hsl(frame)
local color = frame.args[1]
local red = string.sub(color,1,2)
local green = string.sub(color,3,4)
local blue = string.sub(color,5,6)
local rgbcolor = p._hex_to_rgb(red,green,blue)
red = rgbcolor[1]
green = rgbcolor[2]
blue = rgbcolor[3]
return to_text(p._rgb_to_hsl(red,green,blue),'hsl')
end
function p.hsl_to_hex(frame)
local hue = tonumber(frame.args[1])
local sat = tonumber(frame.args[2])
local lum = tonumber(frame.args[3])
local rgbcolor = p._hsl_to_rgb(hue,sat,lum)
local red = rgbcolor[1]
local green = rgbcolor[2]
local blue = rgbcolor[3]
return to_text(p._rgb_to_hex(red,green,blue),'hex')
end
function p.hex_to_jagex(frame)
local color = frame.args[1]
local red = string.sub(color,1,2)
local green = string.sub(color,3,4)
local blue = string.sub(color,5,6)
local rgbcolor = p._hex_to_rgb(red,green,blue)
red = rgbcolor[1]
green = rgbcolor[2]
blue = rgbcolor[3]
return to_text(p._rgb_to_jagex(red,green,blue),'jagex')
end
function p.jagex_to_hex(frame)
local hue = tonumber(frame.args[1])
local sat = tonumber(frame.args[2])
local lum = tonumber(frame.args[3])
local rgbcolor = p._jagex_to_rgb(hue,sat,lum)
local red = rgbcolor[1]
local green = rgbcolor[2]
local blue = rgbcolor[3]
return to_text(p._rgb_to_hex(red,green,blue),'hex')
end
function p._jagex_to_hex(hue,sat,lum)
local rgbcolor = p._jagex_to_rgb(hue,sat,lum)
local red = rgbcolor[1]
local green = rgbcolor[2]
local blue = rgbcolor[3]
return to_text(p._rgb_to_hex(red,green,blue),'hex')
end
--
-- Standardizes return values as a string
--
function to_text(c_args,form)
local formats = {
['hsl'] = 'Hue: '..c_args[1]..' Saturation: '..c_args[2]..' Luminosity: '..c_args[3],
['rgb'] = 'Red: '..c_args[1]..' Green: '..c_args[2]..' Blue: '..c_args[3],
['jagex'] = 'Hue: '..c_args[1]..' Saturation: '..c_args[2]..' Luminosity: '..c_args[3],
['hex'] = c_args[1]..c_args[2]..c_args[3]
}
return formats[form]
end
return p