Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The problem with ColorNameFinder's algorithm #17

Open
pentiumao opened this issue Jul 8, 2019 · 0 comments
Open

The problem with ColorNameFinder's algorithm #17

pentiumao opened this issue Jul 8, 2019 · 0 comments

Comments

@pentiumao
Copy link
Contributor

pentiumao commented Jul 8, 2019

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:

val ndf2 = Math.pow((h - col.hsl.h).toDouble(), 2.0).toInt() + Math.pow((s - col.hsl.s).toDouble(), 2.0).toInt() + Math.pow((l - col.hsl.l).toDouble(), 2.0).toInt()

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant