#!/usr/bin/env python3 import ev3dev.ev3 as ev3 from enum import Enum, unique ''' add enum for color definitions (type should be tupel of int's). later onwards compare the idividual readouts like this to determine the color: RED = (>90, <40, <25) BLUE = (<25, >75, >90) Example: color = (100, 30, 20) (color[0] > 90 and (color[1], color[2]) < (40, 25)) ''' ''' Calculate brightness/to greyscale from RGB: http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=5445596 https://en.wikipedia.org/wiki/YUV Formula: Y=(0.299 x R) + (0.587 x G) + (0.114 x B); ''' class Color(tuple, Enum): RED = (90, 40, 25) BLUE = (25, 75, 90) class Sensor: def __init__(self): self._sensor = ev3.ColorSensor() self._sensor.mode = 'RGB-RAW' self.RGB = self._sensor.bin_data("hhh") self.lastcolor = None def refresh(self): self.RGB = self._sensor.bin_data("hhh") def getbrightness(self): # if(self._sensor.mode == 'COL-REFLECT'): if(self._sensor.mode == 'RGB-RAW'): return ((0.299 * self.RGB[0]) + (0.587 * self.RGB[1]) + (0.114 * self.RGB[2])) # return self._sensor.value() else: print("ERROR: Wrong Sensor Mode.") def getcolor(self): if(self._sensor.mode == 'RGB-RAW'): if(self.RGB[0] > Color.RED[0] and (self.RGB[1], self.RGB[2]) < (Color.RED[1], Color.RED[2])): self.lastcolor = Color.RED if(self.RGB[0] < Color.BLUE[0] and (self.RGB[1], self.RGB[2]) > (Color.BLUE[1], Color.BLUE[2])): self.lastcolor = Color.BLUE def setmode(self, newmode): self._sensor.mode = newmode