Skip to content

Commit a1bae8a

Browse files
committed
feat: hsv color labeling
1 parent a484ef5 commit a1bae8a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

arduino_alvik.py

+40
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,46 @@ def get_color(self, color_format: str = 'rgb') -> (float, float, float):
692692
return self._normalize_color(*self.get_color_raw())
693693
elif color_format == 'hsv':
694694
return self.rgb2hsv(*self._normalize_color(*self.get_color_raw()))
695+
@staticmethod
696+
def get_color_label(h, s, v) -> str:
697+
label = 'UNDEFINED'
698+
699+
if s < 0.1:
700+
if v < 0.05:
701+
label = 'BLACK'
702+
elif v < 0.15:
703+
label = 'GREY'
704+
elif v < 0.8:
705+
label = 'LIGHT GREY'
706+
else:
707+
label = 'WHITE'
708+
else:
709+
if v > 0.1:
710+
if 20 <= h < 90:
711+
label = 'YELLOW'
712+
elif 90 <= h < 140:
713+
label = 'LIGHT GREEN'
714+
elif 140 <= h < 170:
715+
label = 'GREEN'
716+
elif 170 <= h < 210:
717+
label = 'LIGHT BLUE'
718+
elif 210 <= h < 260:
719+
label = 'BLUE'
720+
elif 260 <= h < 280:
721+
label = 'VIOLET'
722+
elif 260 <= h < 280:
723+
label = 'VIOLET'
724+
else: # h<20 or h>=280 is more problematic
725+
if v < 0.5 and s < 0.45:
726+
label = 'BROWN'
727+
else:
728+
if v > 0.77:
729+
label = 'ORANGE'
730+
else:
731+
label = 'RED'
732+
else:
733+
label = 'BLACK'
734+
return label
695735

696736
def get_distance(self, unit: str = 'cm') -> (float, float, float, float, float):
697737
"""

examples/read_color_sensor.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
r, g, b = alvik.get_color()
1111
h, s, v = alvik.get_color('hsv')
1212
print(f'RED: {r}, Green: {g}, Blue: {b}, HUE: {h}, SAT: {s}, VAL: {v}')
13+
print(f'COLOR LABEL: {alvik.get_color_label(h, s, v)}')
1314
sleep_ms(100)
1415
except KeyboardInterrupt as e:
1516
print('over')

0 commit comments

Comments
 (0)