-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratch_hsv_to_rgb3.py
92 lines (74 loc) · 2.74 KB
/
scratch_hsv_to_rgb3.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# ========================================================================
# scratch_hsv_to_rgb3.py
#
# Description: Convert Scratch's HSV values to RGB color.
#
# Author: Jim Ing
# Date: 2024-08-21
# ========================================================================
import argparse
from math import sqrt
from packages.color_names import HTML_COLORS
def scratch_hsv_to_rgb(h, s, v):
h = h * 3.6
s /= 100.0
v /= 100.0
c = v * s
x = c * (1 - abs((h / 60) % 2 - 1))
m = v - c
if 0 <= h < 60:
r, g, b = c, x, 0
elif 60 <= h < 120:
r, g, b = x, c, 0
elif 120 <= h < 180:
r, g, b = 0, c, x
elif 180 <= h < 240:
r, g, b = 0, x, c
elif 240 <= h < 300:
r, g, b = x, 0, c
elif 300 <= h < 360:
r, g, b = c, 0, x
r = int((r + m) * 255)
g = int((g + m) * 255)
b = int((b + m) * 255)
return r, g, b
def closest_html_color_name(rgb):
r, g, b = rgb
closest_color = None
min_distance = float('inf')
for color_name, color_rgb in HTML_COLORS.items():
cr, cg, cb = color_rgb
distance = sqrt((r - cr)**2 + (g - cg)**2 + (b - cb)**2)
if distance < min_distance:
min_distance = distance
closest_color = color_name
return closest_color
def closest_html_color_names(rgb):
r, g, b = rgb
min_distance = float('inf')
closest_colors = []
for color_name, color_rgb in HTML_COLORS.items():
cr, cg, cb = color_rgb
distance = sqrt((r - cr)**2 + (g - cg)**2 + (b - cb)**2)
if distance < min_distance:
min_distance = distance
closest_colors = [color_name] # Start a new list with the closest color
elif distance == min_distance:
closest_colors.append(color_name) # Add to the list if the distance is the same
return closest_colors
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert Scratch HSV to RGB and find the closest HTML color name.")
parser.add_argument("hue", type=float, help="Hue value (0-100 in Scratch)")
parser.add_argument("saturation", type=float, help="Saturation value (0-100 in Scratch)")
parser.add_argument("value", type=float, help="Value (Brightness) value (0-100 in Scratch)")
args = parser.parse_args()
scratch_hue = args.hue
scratch_saturation = args.saturation
scratch_value = args.value
rgb = scratch_hsv_to_rgb(scratch_hue, scratch_saturation, scratch_value)
#html_color_name = closest_html_color_name(rgb)
html_color_name = closest_html_color_names(rgb)
print(f"Scratch HSV: ({scratch_hue}, {scratch_saturation}, {scratch_value})")
print(f"RGB: {rgb}")
print(f"Closest HTML Color Name(s): {html_color_name}")