Jump to content

Categorizing Colors that are in Hex values


Recommended Posts

So I have these color readouts.

9C0007 red

00EE22 green

FF4D00 orange

38303C yellow

00EE22 green

FF4E00 orange

1B171C white

470047 purple

FF4D00 orange

20181F yellow

9C0007 red

C500C5 purple

3B313F blue

470047 purple

A1000A red

00D220 green

C500C5 purple

00EE22 green

FF4D00 orange

56454F blue

302332 blue

00D220 green

A1000A red

00D220 green

00EE22 green

C500C5 purple

5E4C50 blue

453236 blue

9C0007 red

00D220 green

695865 blue

503F4C blue

9C0007 red

4F4441 yellow

453937 yellow

00EE22 green

00EE22 green

A1000A red

2A2324 white

393432 yellow

A700A7 purple

7B7B7B white

7B7B7B white

00E721 green

625555 yellow

453737 blue

36322F yellow

050104 purple

FE4A00 orange

00E721 green

241F1C blue

3F3938 blue

A8000A red

00F524 green

1F1A16 blue

606060 white

FE4A00 orange

00E721 green

A8000A red

221F1C yellow

FE4A00 orange

606060 white

00F524 green

A7000A red

And i'm at a loss of how to catorgize each color into 1,2,3,4,5,6 choices. Like yellows are 1s, reds are 2, and so forth.

Any ideas? The orange and Red I can do with FF, A's and such, but blues and yellows I have no idea.

Link to comment
Share on other sites

This script compares the colour distance of a colour to the colour distance of eight (8) selected colours which are white and black, and the 3 primary colours, red, green, and blue, and the 3 secondary colours cyan, magenta, and yellow.

The minimum distance is the closest colour.

Copy this text to a file. Called it "Color.txt", and put in the script's directory.

FFFFFF WHITE
FF0000 RED
00FF00 GREEN
0000FF Blue
00FFFF CYAN
FF00FF MAGENTA
FFFF00 YELLOW
000000 BLACK
9C0007 red
00EE22 green
FF4D00 orange
38303C yellow
00EE22 green
FF4E00 orange
1B171C white
470047 purple
FF4D00 orange
20181F yellow
9C0007 red
C500C5 purple
3B313F blue
470047 purple
A1000A red
00D220 green
C500C5 purple
00EE22 green
FF4D00 orange
56454F blue
302332 blue
00D220 green
A1000A red
00D220 green
00EE22 green
C500C5 purple
5E4C50 blue
453236 blue
9C0007 red
00D220 green
695865 blue
503F4C blue
9C0007 red
4F4441 yellow
453937 yellow
00EE22 green
00EE22 green
A1000A red
2A2324 white
393432 yellow
A700A7 purple
7B7B7B white
7B7B7B white
00E721 green
625555 yellow
453737 blue
36322F yellow
050104 purple
FE4A00 orange
00E721 green
241F1C blue
3F3938 blue
A8000A red
00F524 green
1F1A16 blue
606060 white
FE4A00 orange
00E721 green
A8000A red
221F1C yellow
FE4A00 orange
606060 white
00F524 green
A7000A red
When the script is run,the colour before the "/" is the colour assigned to it in the first post (except for the first 8 colours). The last colour after the "/" is the estimated colour.

The script.

;#include <Array.au3>
;#include <GUIConstantsEx.au3>

;Opt('MustDeclareVars', 1)

Local $sStr = StringStripCR(FileRead("Color.txt"));"F:\Full Path & Name\CVSFile.cvs")
Local $iNumLF, $aLines, $iNum = 1
Local $widthCell, $msg, $iOldOpt, $iXPos = 10, $iYPos = 10

; Number of lines in file, or, rows in 2D array
StringReplace($sStr, @LF, @LF)
$iNumLF = @extended
;ConsoleWrite($iNumLF & @CRLF)

$aLines = StringSplit(StringStripWS($sStr, 3), " " & @LF, 0)
;_ArrayDisplay($aLines)

GUICreate("Estimate Colour", 740, 900, -1, -1, 0x00040000) ; will create a dialog box that when displayed is centered
GUISetBkColor(0xFFFFF8)
GUISetFont(10, 600)
For $i = 1 To UBound($aLines) - 2 Step 2
    If $i >= $aLines[0] / 2 Then
        $iXPos = 360
        $iYPos = -($aLines[0] / 2 * 12) + 10
    EndIf
    GUICtrlCreateLabel("", $iXPos, ($i - 1) * 12 + $iYPos, 100, 20, -1, 0x00000001) ; first cell 70 width
    GUICtrlSetBkColor(-1, "0x" & $aLines[$i])
    GUICtrlSetResizing(-1, 802)
    GUICtrlCreateLabel("0x" & $aLines[$i] & " " & $aLines[$i + 1] & "/" & _ColorEstimate("0x" & $aLines[$i]), _
            $iXPos + 105, ($i - 1) * 12 + $iYPos, $iXPos + 220, 20) ; next line
    GUICtrlSetResizing(-1, 802)
Next
GUISetState()

Do
    $msg = GUIGetMsg()
Until $msg = -3

Func _ColorEstimate($Col)
    local $iDist
    Local $aC[9][2] = [["FFFFFF", "WHITE"],["FF0000", "RED"],["00FF00", "GREEN"],["0000FF", "Blue"], _
            ["00FFFF", "CYAN"],["FF00FF", "MAGENTA"],["FFFF00", "YELLOW"],["000000", "BLACK"],["", "WHITE"]]
    Local $Min = _ColourDist($Col, $aC[0][0])
    For $i = 1 To 7
        $iDist = _ColourDist($Col, $aC[$i][0])
        If $iDist < $Min Then
            $Min = $iDist
            $aC[8][1] = $aC[$i][1]
        EndIf
    Next
    Return $aC[8][1]
EndFunc ;==>_ColorEstimate

Func _ColourDist($sColA, $sColB)
    If IsString($sColA) Then
        If StringRegExp($sColA, "(?i)(0x.{6})") Then $sColA = StringTrimLeft($sColA, 2)
    Else
        $sColA = Hex($sColA, 6)
    EndIf
    ;ConsoleWrite($sColA & " ")
    If IsString($sColB) Then
        If StringRegExp($sColB, "(?i)(0x.{6})") Then $sColB = StringTrimLeft($sColB, 2)
    Else
        $sColB = Hex($sColB, 6)
    EndIf
    ;ConsoleWrite($sColB & @CRLF)
    Return Execute(StringRegExpReplace($sColA & $sColB, "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})", _
            'Sqrt(("0x\4" - "0x\1") ^ 2 + ("0x\5" - "0x\2") ^ 2 + ("0x\6" - "0x\3") ^ 2)'))
EndFunc ;==>_ColourDist

Edit: Ref: #787474

Edited by Malkey
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...