Skip to content

Commit a484ef5

Browse files
committed
feat: color rgb, hsv and normalization
1 parent 20a492a commit a484ef5

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

arduino_alvik.py

+87-3
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,22 @@ def get_touch_right(self) -> bool:
533533
"""
534534
return bool(self.touch_bits & 0b10000000)
535535

536+
@staticmethod
537+
def _limit(value: float, lower: float, upper: float) -> float:
538+
"""
539+
Utility function to limit a value between a lower and upper limit
540+
:param value:
541+
:param lower:
542+
:param upper:
543+
:return:
544+
"""
545+
assert lower < upper
546+
if value > upper:
547+
value = upper
548+
if value < lower:
549+
value = lower
550+
return value
551+
536552
def _set_color_reference(self):
537553
try:
538554
from color_calibration import BLACK_CAL as _B
@@ -605,9 +621,77 @@ def get_color_raw(self) -> (int, int, int):
605621
"""
606622

607623
return self.red, self.green, self.blue
608-
# return (int((self.red/COLOR_FULL_SCALE)*255),
609-
# int((self.green/COLOR_FULL_SCALE)*255),
610-
# int((self.blue/COLOR_FULL_SCALE)*255))
624+
625+
def _normalize_color(self, r: float, g: float, b: float) -> (float, float, float):
626+
"""
627+
Color normalization
628+
:param r:
629+
:param g:
630+
:param b:
631+
:return:
632+
"""
633+
r = self._limit(r, self._black_cal[0], self._white_cal[0])
634+
g = self._limit(g, self._black_cal[1], self._white_cal[1])
635+
b = self._limit(b, self._black_cal[2], self._white_cal[2])
636+
637+
r = (r - self._black_cal[0])/(self._white_cal[0] - self._black_cal[0])
638+
g = (g - self._black_cal[1])/(self._white_cal[1] - self._black_cal[1])
639+
b = (b - self._black_cal[2])/(self._white_cal[2] - self._black_cal[2])
640+
641+
return r, g, b
642+
643+
@staticmethod
644+
def rgb2hsv(r: float, g: float, b: float) -> (float, float, float):
645+
"""
646+
Converts normalized rgb to hsv
647+
:param r:
648+
:param g:
649+
:param b:
650+
:return:
651+
"""
652+
min_ = min(r, g, b)
653+
max_ = max(r, g, b)
654+
655+
v = max_
656+
delta = max_ - min_
657+
658+
if delta < 0.00001:
659+
h = 0
660+
s = 0
661+
return h, s, v
662+
663+
if max_ > 0:
664+
s = delta / max_
665+
else:
666+
s = 0
667+
h = None
668+
return h, s, v
669+
670+
if r >= max_:
671+
h = (g - b) / delta # color is between yellow and magenta
672+
elif g >= max_:
673+
h = 2.0 + (b - r) / delta
674+
else:
675+
h = 4.0 + (r - g) / delta
676+
677+
h *= 60.0
678+
if h < 0:
679+
h += 360.0
680+
681+
return h, s, v
682+
683+
def get_color(self, color_format: str = 'rgb') -> (float, float, float):
684+
"""
685+
Returns the normalized color readout of the color sensor
686+
:param color_format: rgb or hsv only
687+
:return:
688+
"""
689+
assert color_format in ['rgb', 'hsv']
690+
691+
if color_format == 'rgb':
692+
return self._normalize_color(*self.get_color_raw())
693+
elif color_format == 'hsv':
694+
return self.rgb2hsv(*self._normalize_color(*self.get_color_raw()))
611695

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

examples/read_color_sensor.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
alvik = ArduinoAlvik()
66
alvik.begin()
7-
speed = 0
87

98
while True:
109
try:
11-
r, g, b = alvik.get_color_raw()
12-
print(f'RED: {r}, Green: {g}, Blue: {b}')
10+
r, g, b = alvik.get_color()
11+
h, s, v = alvik.get_color('hsv')
12+
print(f'RED: {r}, Green: {g}, Blue: {b}, HUE: {h}, SAT: {s}, VAL: {v}')
1313
sleep_ms(100)
1414
except KeyboardInterrupt as e:
1515
print('over')

0 commit comments

Comments
 (0)