@@ -533,6 +533,22 @@ def get_touch_right(self) -> bool:
533
533
"""
534
534
return bool (self .touch_bits & 0b10000000 )
535
535
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
+
536
552
def _set_color_reference (self ):
537
553
try :
538
554
from color_calibration import BLACK_CAL as _B
@@ -605,9 +621,77 @@ def get_color_raw(self) -> (int, int, int):
605
621
"""
606
622
607
623
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 ()))
611
695
612
696
def get_distance (self , unit : str = 'cm' ) -> (float , float , float , float , float ):
613
697
"""
0 commit comments