Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Closest Color Match
#1
This here is one of those tools that was so damn simple to make, and one which I think I could've used a bazillion times in the past.  I have to wonder why the heck I haven't made something like this earlier.  GAHHH!!! 

Anywho, here is the amazing color matcher!

Code: (Select All)
_Title "Steve's Color Match Tool"
Screen _NewImage(800, 800, 32)
Randomize Timer

Open "Color Name Large List.txt" For Binary As #1
Do Until EOF(1)
    Line Input #1, junk$ 'just to count lines
    total = total + 1
Loop

Close: Open "Color Name Large List.txt" For Input As #1

Dim Names(total) As String, Kolors(total) As _Unsigned Long
Dim test As _Unsigned Long

For i = 1 To total
    Input #1, Names(i), r, g, b, a
    Kolors(i) = _RGBA32(r, g, b, a)
Next

Print "Matching against "; total; "colors in list."
For i = 1 To 10
    test = _RGB32(Rnd * 256, Rnd * 256, Rnd * 256)
    closest = ClosestColorIndex(test, Kolors())
    Print Names(closest); " is the closest match", "("; _Red32(test); ","; _Green32(test); ","; _Blue32(test); ")", "("; _Red32(Kolors(closest)); ","; _Green32(Kolors(closest)); ","; _Blue32(Kolors(closest)); ")"
    Line (100, 150 + 50 * i)-Step(100, 50), test, BF
    Line (220, 150 + 50 * i)-Step(100, 50), Kolors(closest), BF
Next

Function ClosestColorIndex& (targetColor As _Unsigned Long, Pal() As _Unsigned Long)
    Dim As Long tr, tg, tb, colors, bestdist
    Dim As Long bestIndex, dr, dg, db, i, dist
    Dim As _Unsigned Long c

    tr = _Red32(targetColor): tg = _Green32(targetColor): tb = _Blue32(targetColor):
    colors = UBound(Pal): bestdist = _LONG_MAX

    $Checking:Off
    While i < colors
        c = Pal(i)
        dr = tr - _Red32(c)
        dg = tg - _Green32(c)
        db = tb - _Blue32(c)
        dist = dr * dr + dg * dg + db * db
        If dist < bestdist Then
            If dist = 0 Then ClosestColorIndex = i: Exit Function
            bestdist = dist: bestIndex = i
        End If
        i = i + 1
    Wend
    $Checking:On
    ClosestColorIndex = bestIndex
End Function


As you can see, this chooses 10 random colors from us.
Then it runs those 10 random colors against the list of 270 colors which are our color constants.
Then it tells you which of the color constants are the closest match to your specific color.

Then it draws you a box so you can compare with your own eyes how close the match is to the original color.

And I'll let the results speak for themselves!



Edit:  Updated to optomize with the changes as suggested below.
Edit:  Also updated to use STxATxIC's large list of gathered colored names from across the interweb.  It's almost 10,000 names in the list so it's pretty comprehensive and does a good job at matching colors extremely closely to one of these many given names.


Attached Files
.txt   Color Name Large List.txt (Size: 322.41 KB / Downloads: 13)
Reply


Messages In This Thread
Closest Color Match - by SMcNeill - 01-18-2026, 03:53 AM
RE: Closest Color Match - by Pete - 01-18-2026, 04:12 AM
RE: Closest Color Match - by SMcNeill - 01-18-2026, 04:19 AM
RE: Closest Color Match - by Pete - 01-18-2026, 06:04 AM
RE: Closest Color Match - by a740g - 01-18-2026, 03:36 PM
RE: Closest Color Match - by SMcNeill - 01-18-2026, 07:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Color Picker TSR SMcNeill 5 1,389 01-24-2025, 07:31 PM
Last Post: Pete
  Color Picker SMcNeill 11 2,546 01-24-2025, 01:13 AM
Last Post: SMcNeill
  Color Fetch Tool SMcNeill 0 641 11-25-2022, 11:31 AM
Last Post: SMcNeill
  Convert 32-bit image to 256 color SMcNeill 0 956 05-01-2022, 05:45 AM
Last Post: SMcNeill

Forum Jump:


Users browsing this thread: 1 Guest(s)