File: color-names

package info (click to toggle)
xournalpp 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,044 kB
  • sloc: cpp: 64,195; xml: 939; sh: 752; ansic: 362; python: 338; php: 74; makefile: 15
file content (47 lines) | stat: -rwxr-xr-x 1,356 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#! /bin/python3

from functools import partial
from math import sqrt
from sys import argv

from matplotlib.colors import get_named_colors_mapping

no_colors_to_display = 20
usage = f"""{argv[0]} [ HEX_CODE_RGB ]
    shows the {no_colors_to_display} css4 colors that are closest
    to the argument by euclidean distance.
    """

def hex_to_rgb(hex_str):
    hexval = int(hex_str, 16)
    return (hexval >> 16, (hexval >> 8) & 0xff, hexval & 0xff)

css4_colors = map(lambda t: (t[0], hex_to_rgb(t[1][1:])),
                filter(lambda t: len(t[0]) > 1 and not t[0].startswith("xkcd"),
                get_named_colors_mapping().items()))

def eucl_dst(v1, v2):
    return sqrt(sum([(v1_i - v2_i) ** 2 for (v1_i, v2_i) in zip(v1, v2)]))



if __name__  == '__main__':
    if (len(argv) != 2 or argv[1] in ('-h', '--help')):
        print(usage)
        exit(0)

    point = None
    try:
        point = hex_to_rgb(argv[1])
    except Exception:
        print(f"Invalid hex code\n{usage}")
        exit(1)
    dist_from_point = partial(eucl_dst, point)
    print("Point: {}".format(point))
    for key, val in \
            sorted(css4_colors,
                key = lambda t: dist_from_point(t[1]))[:no_colors_to_display]:
        print("dist = {} color: {} code: {}".format(
            round(dist_from_point(val), 2),
            key,
            val))