Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created October 28, 2022 23:28
Show Gist options
  • Save howmanysmall/dd17bb27ce50601e48f01a9397076d5b to your computer and use it in GitHub Desktop.
Save howmanysmall/dd17bb27ce50601e48f01a9397076d5b to your computer and use it in GitHub Desktop.
local function ConvertToHSLColor(Color: Color3)
local Min = math.min(Color.R*255, Color.G*255, Color.B*255)/255
local Max = math.max(Color.R*255, Color.G*255, Color.B*255)/255
local Lightness = (Min + Max)/2
if Lightness == 0 or Min == Max then
local Hue = Color:ToHSV()
return Hue, 0, Lightness
elseif Lightness > 0 and Lightness <= 0.5 then
local Hue = Color:ToHSV()
return Hue, (Max - Min)/(Max + Min), Lightness
else
local Hue = Color:ToHSV()
return Hue, (Max - Min)/(2 - (Max + Min)), Lightness
end
end
local HUE_LIMITS_FOR_SATURATION_LEVEL_1 =
{8, 0, 0, 44, 0, 0, 0, 63, 0, 0, 122, 0, 134, 0, 0, 0, 0, 166, 176, 241, 0, 256, 0}
local HUE_LIMITS_FOR_SATURATION_LEVEL_2 =
{0, 10, 0, 32, 46, 0, 0, 0, 61, 0, 106, 0, 136, 144, 0, 0, 0, 158, 166, 241, 0, 0, 256}
local HUE_LIMITS_FOR_SATURATION_LEVEL_3 =
{0, 8, 0, 0, 39, 46, 0, 0, 0, 71, 120, 0, 131, 144, 0, 0, 163, 0, 177, 211, 249, 0, 256}
local HUE_LIMITS_FOR_SATURATION_LEVEL_4 =
{0, 11, 26, 0, 0, 38, 45, 0, 0, 56, 100, 121, 129, 0, 140, 0, 180, 0, 0, 224, 241, 0, 256}
local HUE_LIMITS_FOR_SATURATION_LEVEL_5 =
{0, 13, 27, 0, 0, 36, 45, 0, 0, 59, 118, 0, 127, 136, 142, 0, 185, 0, 0, 216, 239, 0, 256}
local LUMINOSITY_LIMITS_FOR_HUE_INDEX_LOW = {
130,
100,
115,
100,
100,
100,
110,
75,
100,
90,
100,
100,
100,
100,
80,
100,
100,
100,
100,
100,
100,
100,
100,
}
local LUMINOSITY_LIMITS_FOR_HUE_INDEX_HIGH = {
170,
170,
170,
155,
170,
170,
170,
170,
170,
115,
170,
170,
170,
170,
170,
170,
170,
170,
150,
150,
170,
140,
165,
}
local COLOR_NAMES_LIGHT = {
"Coral",
"Rose",
"Light Orange",
"Tan",
"Tan",
"Light Yellow",
"Light Yellow",
"Tan",
"Light Green",
"Lime",
"Light Green",
"Light Green",
"Aqua",
"Sky Blue",
"Light Turquoise",
"Pale Blue",
"Light Blue",
"Ice Blue",
"Periwinkle",
"Lavender",
"Pink",
"Tan",
"Rose",
}
local COLOR_NAMES_MID = {
"Coral",
"Red",
"Orange",
"Brown",
"Tan",
"Gold",
"Yellow",
"Olive Green",
"Olive Green",
"Green",
"Green",
"Bright Green",
"Teal",
"Aqua",
"Turquoise",
"Pale Blue",
"Blue",
"Blue Gray",
"Indigo",
"Purple",
"Pink",
"Brown",
"Red",
}
local COLOR_NAMES_DARK = {
"Brown",
"Dark Red",
"Brown",
"Brown",
"Brown",
"Dark Yellow",
"Dark Yellow",
"Brown",
"Dark Green",
"Dark Green",
"Dark Green",
"Dark Green",
"Dark Teal",
"Dark Teal",
"Dark Teal",
"Dark Blue",
"Dark Blue",
"Blue Gray",
"Indigo",
"Dark Purple",
"Plum",
"Brown",
"Dark Red",
}
local function DetermineColor(Color: Color3)
local Hue, Saturation, Luminosity = ConvertToHSLColor(Color)
Hue = (if Hue == 0 then 0 else Hue/360)*255
Saturation *= 255
Luminosity *= 255
if Luminosity > 240 then
return "White"
elseif Luminosity < 20 then
return "Black"
end
if Saturation <= 20 then
if Luminosity > 170 then
return "Light Gray"
elseif Luminosity > 100 then
return "Gray"
else
return "Dark Gray"
end
end
local HueLimits
if Saturation > 20 and Saturation <= 75 then
HueLimits = HUE_LIMITS_FOR_SATURATION_LEVEL_1
elseif Saturation > 75 and Saturation <= 115 then
HueLimits = HUE_LIMITS_FOR_SATURATION_LEVEL_2
elseif Saturation > 115 and Saturation <= 150 then
HueLimits = HUE_LIMITS_FOR_SATURATION_LEVEL_3
elseif Saturation > 150 and Saturation <= 240 then
HueLimits = HUE_LIMITS_FOR_SATURATION_LEVEL_4
else
HueLimits = HUE_LIMITS_FOR_SATURATION_LEVEL_5
end
local ColorIndex = 0
for Index = 1, #COLOR_NAMES_MID do
if Hue < HueLimits[Index] then
ColorIndex = Index
break
end
end
if ColorIndex ~= 0 then
if Luminosity > LUMINOSITY_LIMITS_FOR_HUE_INDEX_HIGH[ColorIndex] then
return COLOR_NAMES_LIGHT[ColorIndex]
elseif Luminosity < LUMINOSITY_LIMITS_FOR_HUE_INDEX_LOW[ColorIndex] then
return COLOR_NAMES_DARK[ColorIndex]
else
return COLOR_NAMES_MID[ColorIndex]
end
end
return "Unknown"
end
return DetermineColor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment