Groovy web console

subscribe to the feed Subscribe
to this
site

Get approximate color from db32

Published 3 months ago by cuongtvc with tags color paint.net
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
def getNumber(String char1, String char2) {
    def convertMap = [
        '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15
    ]
    return convertMap[char1.toUpperCase()] * 16 + convertMap[char2.toUpperCase()]
}
def colors = [
    '000000', 
    '222034',
    '45283C',
    '663931',
    '8F563B',
    'DF7126',
    'D9A066',
    'EEC39A',
    'FBF236',
    '99E550',
    '6ABE30',
    '37946E',
    '4B692F',
    '524B24',
    '323C39',
    '3F3F74',
    '306082',
    '5B6EE1',
    '639BFF',
    '5FCDE4',
    'CBDBFC',
    'FFFFFF',
    '9BADB7',
    '847E87',
    '696A6A',
    '595652',
    '76428A',
    'AC3232',
    'D95763',
    'D77BBA',
    '8F974A',
    '8A6F30'
]
def testColor = '6C9DCE'

def min = 1_000_000
def bestColor;
for (def color: colors) {
    def current =  (Math.pow(getNumber(color[0], color[1]) - getNumber(testColor[0], testColor[1]), 2) 
        + Math.pow(getNumber(color[2], color[3]) - getNumber(testColor[2], testColor[3]), 2) 
        + Math.pow(getNumber(color[4], color[5]) - getNumber(testColor[4], testColor[5]), 2))
    if (current < min) {
        min = current;
        bestColor = color
    }
}
println bestColor