#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Table is reading as a Number

in Bugs Posts: 5

The variable CurrentHSL at the end is apparently a number when it should be a table (returned by the function HSL). I'm getting the error when the last line indexes CurrentHSL.H

``````    Round = function(Number)
return Number % 1 < 0.5 and math.floor(Number) or math.ciel(Number)
end

Complement = function(...)
r, g, b = RGB(...)
return 255 - r, 255 - g, 255 - b
end

RGB = function(...)
local Arguments, r, g, b = {...}
if #Arguments == 0 then
return
elseif #Arguments == 1 then
local Argument, ArgType = ..., type(...)
if ArgType == 'string' then
local HexSring = Argument:match('^#%x%x%x%x%x%x\$')
if Hex then -- if it's a valid hexidecimal color code
Hex = Hex:gsub('%X', '')
r = tonumber('0x'..Hex:sub(1, 2))
g = tonumber('0x'..Hes:sub(3, 4))
b = tonumber('0x'..Hex.sub(5))
end
elseif ArgType == 'userdata' and Argument.r then
r, g, b = Argument.r, Argument.g, Argument.b
elseif ArgType == 'table' then
if Argument.Type and Argument.Type == 'HSL' then
local h, s, l  = Argument.Hue, Argument.Saturation, Argument.Luminance
if s == 0 then
l = l * 255
r, g, b = l, l, l
else
local temp1, temp2, tempR, tempG, tempB
if l < .5 then
temp1 = l * (1 + s)
else
temp1 = l + s - l * s
end

temp2 = 2 * l - temp1
h = h / 360
tempR = h + 0.333
tempG = h
tempB = h - 0.333

if 6 * tempR < 1 then
r = temp2 + (temp1 - temp2) * 6 * tempR
elseif 2 * tempR < 1 then
r = temp1
elseif 3 * tempR < 2 then
r = temp2 + (temp1 - temp2) * (0.666 - tempR) * 6
else
r = temp2
end

if 6 * tempG < 1 then
g = temp2 + (temp1 - temp2) * 6 * tempG
elseif 2 * tempG < 1 then
g = temp1
elseif 3 * tempG < 2 then
g = temp2 + (temp1 - temp2) * (0.666 - tempG) * 6
else
g = temp2
end

if 6 * tempB < 1 then
b = temp2 + (temp1 - temp2) * 6 * tempB
elseif 2 * tempB < 1 then
b = temp1
elseif 3 * tempB < 2 then
b = temp2 + (temp1 - temp2) * (0.666 - tempB) * 6
else
b = temp2
end

r, g, b = r * 255, g * 255, b * 255
end
end
-- I'll figure out HSV later.
end
else
local First, ArgType
for Index, Item in pairs(Arguments) do
if Index == 1 then
First, ArgType = type(Item), type(Item)
elseif type(Item) ~= First then
ArgType = 'varying'
break
end
end
if ArgType == 'number' then
if #Arguments == 3 then
for Index, Item in pairs(Arguments) do
Arguments[Index] = math.min(255, Item)
end
r, g, b = Arguments[1], Arguments[2], Arguments[3]
end
end
end
return Round(r), Round(g), Round(b)
end

HexDigits = {
[0] = '0';
[1] = '1';
[2] = '2';
[3] = '3';
[4] = '4';
[5] = '5';
[6] = '6';
[7] = '7';
[8] = '8';
[9] = '9';
[10] = 'A';
[11] = 'B';
[12] = 'C';
[13] = 'D';
[14] = 'E';
[15] = 'F';
}

Hex = function(...)
rgb = {RGB(...)}
local String = '#'
for _, Number in pairs(rgb) do
local Modulus = Number % 16
Number = math.floor(math.abs(Number)/16)
String = String..HexDigits[Number]..HexDigits[Modulus]
end
return String
end

HSL = function(...)
r, g, b = RGB(...)
r, g, b = math.abs(r), math.abs(g), math.abs(b)

while r > 1 do
r = r / 255
end
while g > 1 do
g = g / 255
end
while b > 1 do
b = b / 255
end

local min, max = math.min(r, g, b), math.max(r, g, b)
local luminance = (min + max) / 2

if min == max then
return 0, 0, luminance
end

local saturation
if min == max then
saturation = 0
elseif luminance < 0.5 then
saturation = (max - min)/(max + min)
else
saturation = (max - min)/(2 - max - min)
end

local hue

if r >= g and g >= b then
hue = (g - b)/(r - b)
elseif g > r and r >= b then
hue = 2 - ((r - b)/(g - b))
elseif g >= b and b > r then
hue = 2 + ((b - r)/(g - r))
elseif b > g and g > r then
hue = 4 - ((g - r)/(b - r))
elseif b > r and r >= g then
hue = 4 + ((r - g)/(b - g))
elseif r >= b and b > g then
hue = 6 - ((b - g)/(r - g))
end

hue = hue * 60

while hue < 0 do
hue = hue + 360
end
while hue > 360 do
hue = hue - 360
end

return {
Type = 'HSL';
Hue = hue;
H = hue;
Saturation = saturation;
S = Saturation;
Luminance = luminance;
L = luminance;
}

end

R, G, B = 255, 255, 255
CurrentHSL = HSL(R, G,
H, S, L = CurrentHSL.H, CurrentHSL.S, CurrentHSL.L
``````
Tagged:

• Mod
Posts: 5,396

Well, for one thing, when min==max, you return three separate values instead of a table....

• Posts: 5

Thank you @Ignatz! I missed that part. I'll check if it fixes it or not.

• Posts: 5

It works!