Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

find closest color python

from math import sqrt

COLORS = (
    (181, 230, 99),
    (23, 186, 241),
    (99, 23, 153),
    (231, 99, 29),
)

def closest_color(rgb):
    r, g, b = rgb
    color_diffs = []
    for color in COLORS:
        cr, cg, cb = color
        color_diff = sqrt(abs(r - cr)**2 + abs(g - cg)**2 + abs(b - cb)**2)
        color_diffs.append((color_diff, color))
    return min(color_diffs)[1]

closest_color((12, 34, 156))
# => (99, 23, 153)

closest_color((23, 145, 234))
# => (23, 186, 241)
Comment

PREVIOUS NEXT
Code Example
Python :: drop missing values in a column pandas 
Python :: python 3.9 features 
Python :: python save output to file 
Python :: raising exceptions in python 
Python :: send message if user is banned discord.py 
Python :: how to pair up two lists in python 
Python :: python [remote rejected] master - master (pre-receive hook declined) 
Python :: delete the content from the entry form in tkinter python 
Python :: pyttsx3 female voice template 
Python :: create 2d array python list comprehension 
Python :: python input 
Python :: python get element from list 
Python :: python remove all unicode from string 
Python :: How to convert simple string in to camel case in python 
Python :: cut part of video ffmpeg 
Python :: python remove last element from list 
Python :: smtpauthenticationerror 
Python :: python get os 
Python :: current date to epoch python 
Python :: python chat application 
Python :: numpy empty image 
Python :: Count NaN values of an DataFrame 
Python :: matplotlib secondary y axis 
Python :: change directory in python script 
Python :: python get value from decimal object 
Python :: find largest 10 number in dataframe 
Python :: power level in google colab 
Python :: timestamp e datetime python 
Python :: python snakes 
Python :: how to sum only the even values in python 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =