name-that-color-intellij-plugin
name-that-color-intellij-plugin copied to clipboard
The problem with ColorNameFinder's algorithm
I found one problem when I name the color of #444444.
"mine_shaft" was given by plugin, the same as the color of #222222, but they are a little bit different.
Then I input #444444 on the website name that color, but I got the name of "Tundora".
Steps to reproduce:
println(ColorNameFinder.findColor(HexColor("#444444")))
Actual result:
(#444444, Color(hexCode=323232, name=Mine Shaft, rgb=Rgb(r=50, g=50, b=50), hsl=Hsl(h=0, s=0, l=20)))
Expected result:
(#444444, Color(hexCode=4A4244, name=Tundora, rgb=Rgb(r=74, g=66, b=68), hsl=Hsl(h=345, s=6, l=27)))
I think the problem is at this line after I checked the source of ntc:
https://github.com/galex/name-that-color-intellij-plugin/blob/69d18a19ad134b3d627aef1eda968adece1d260f/namethatcolor/src/main/kotlin/il/co/galex/namethatcolor/core/manager/ColorNameFinder.kt#L46
Let's look at the HSL value of three colors:
#4A4244 Rgb(r=74, g=66, b=68) Hsl(h=345, s=6, l=27)
#323232 Rgb(r=50, g=50, b=50) Hsl(h=0, s=0, l=20)
#444444 Rgb(r=68, g=68, b=68) Hsl(h=0, s=0, l=27)
Because H in HSL represents a degree, the delta value between #4A4244 and #444444 should be 15 but not 345. As a result, it makes #323232 be the closest color to #444444.
The fix should be something like this:
private fun deltaOfTwoDegree(l: Int, r: Int): Int {
var delta = abs(l - r)
if (delta >= 180) {
delta = (360 - delta)
}
return delta
}
Hope it helps.