Module Color
[frames] | no frames]

Source Code for Module Color

  1  # Color.py 
  2   
  3  ''' 
  4   This software is part of the EV3BrickPi library. 
  5   It is Open Source Free Software, so you may 
  6   - run the code for any purpose 
  7   - study how the code works and adapt it to your needs 
  8   - integrate all or parts of the code in your own programs 
  9   - redistribute copies of the code 
 10   - improve the code and release your improvements to the public 
 11   However the use of the code is entirely your responsibility. 
 12  ''' 
 13   
14 -class Color():
15 # BLACK, BLUE, GREEN, YELLOW, RED, WHITE 16 ''' 17 Standard values for EV3 color sensor may be modified to suit your situation. 18 ''' 19 colorCubes = [[0, 15, 9, 25, 3, 13], [1, 30, 20, 80, 30, 110], 20 [30, 100, 50, 180, 10, 40], [150, 300, 100, 330, 20, 70], 21 [100, 300, 10, 60, 10, 60], [100, 400, 100, 400, 100, 400]] 22 23 ''' 24 Standard names from brickpi3 library 25 ''' 26 colorNames = ["Black", "Blue", "Green", "Yellow", "Red", "White"] 27
28 - def __init__(self, value):
29 ''' 30 Creates a Color instance from [red, green, blue] 31 red, green, blue: RGB values 0..255 32 ''' 33 self.red = value[0] 34 self.green = value[1] 35 self.blue = value[2] 36 self.colorInt = (self.red << 16) + (self.green << 8) + self.blue
37
38 - def getColorInt(self):
39 ''' 40 Returns color as integer: bits 0..7: blue, bits 8..15: green, bits 16..23: red. 41 ''' 42 return self.colorInt
43
44 - def getRed(self):
45 ''' 46 Returns red RGB value 0..255. 47 ''' 48 return self.red
49
50 - def getGreen(self):
51 ''' 52 Returns green RGB value 0..255. 53 ''' 54 return self.green
55
56 - def getBlue(self):
57 ''' 58 Returns blue RGB value 0..255. 59 ''' 60 return self.blue
61
62 - def getRGB(self):
63 ''' 64 Returns list of RGB values 0..255. 65 ''' 66 return [self.red, self.green, self.blue]
67
68 - def getColorID(self):
69 ''' 70 Returns color cube id 0..5 71 ''' 72 for i in range(6): 73 if self._inColorCube(self, Color.colorCubes[i]): 74 return i + 1 75 return 0
76
77 - def getColorLabel(self):
78 ''' 79 Returns color name determined with color cubes. Adapt the color cubes to your situation. 80 ''' 81 id = self.getColorID() 82 if id == 1: 83 return "BLACK" 84 elif id == 2: 85 return "BLUE" 86 elif id == 3: 87 return "GREEN" 88 elif id == 4: 89 return "YELLOW" 90 elif id == 5: 91 return "RED" 92 elif id == 6: 93 return "WHITE" 94 else: 95 return "UNDEFINED"
96
97 - def _inColorCube(self, color, colorCube):
98 if (self.red >= colorCube[0] 99 and self.red <= colorCube[1] 100 and self.green >= colorCube[2] 101 and self.green <= colorCube[3] 102 and self.blue >= colorCube[4] 103 and self.blue <= colorCube[5]): 104 return True 105 return False
106
107 - def __repr__(self):
108 return "RGB: " + str(self.getRGB())
109