IDS peak ICV v1.2.0
Loading...
Searching...
No Matches
color.py
Go to the documentation of this file.
1from __future__ import annotations
2
3from enum import Enum
4from typing import Any
5
6from ids_peak_icv.exceptions import OutOfRangeException, NotPossibleException, NotSupportedException
7
8
9class ColorConstant(Enum):
10 """!
11 @brief
12 Enum class for color constants.
13
14 @ingroup ids_peak_icv_python_painting
15 @since ids_peak_icv 1.1
16 """
17
18 BLACK = 0
19 WHITE = 1
20 RED = 2
21 GREEN = 3
22 BLUE = 4
23 DIM_GRAY = 5
24 GRAY = 6
25 LIGHT_GRAY = 7
26 CYAN = 8
27 MAGENTA = 9
28 YELLOW = 10
29 MEDIUM_SLATE_BLUE = 11
30 CORAL = 12
31 SLATE_BLUE = 13
32 SPRING_GREEN = 14
33 ORANGE_RED = 15
34 DARK_OLIVE_GREEN = 16
35 PINK = 17
36 CADET_BLUE = 18
37 GOLDENROD = 19
38 ORANGE = 20
39 GOLD = 21
40 FOREST_GREEN = 22
41 CORNFLOWER_BLUE = 23
42 NAVY = 24
43 TURQUOISE = 25
44 DARK_SLATE_BLUE = 26
45 LIGHT_BLUE = 27
46 INDIAN_RED = 28
47 VIOLET_RED = 29
48 LIGHT_STEEL_BLUE = 30
49 MEDIUM_BLUE = 31
50 KHAKI = 32
51 VIOLET = 33
52 FIREBRICK = 34
53 MIDNIGHT_BLUE = 35
54
55
57 """!
58 @brief
59 Enum class for color palette constants.
60
61 @since ids_peak_icv 1.0
62 @ingroup ids_peak_icv_python_painting
63 """
64
65 COLORED_3 = 0
66 """!
67 @brief
68 3 color palette (red, green, blue)
69
70 @since ids_peak_icv 1.0
71 """
72
73 COLORED_6 = 1
74 """!
75 @brief
76 6 color palette (red, green, blue, yellow, magenta, cyan)
77
78 @since ids_peak_icv 1.0
79 """
80
81
82class Color:
83 """!
84 @brief
85 Class holding color codes.
86
87 @since ids_peak_icv 1.0
88 @ingroup ids_peak_icv_python_painting
89 """
90
91 @classmethod
92 def create_from_rgb(cls, red: int, green: int, blue: int, num_bits_per_pixel: int = 8) -> Color:
93 """!
94 @brief
95 Creates a color from red, green and blue values.
96
97 The values must be between 0 and 255 and will be automatically adapted to the given bit range.
98
99 @param red: Red value.
100 @param green: Green value.
101 @param blue: Blue value.
102 @param num_bits_per_pixel: Number of bits per pixel. Defaults to 8.
103
104 @exception OutOfRangeException: If the color values are out of range.
105
106 @return A Color object.
107
108 @since ids_peak_icv 1.0
109 """
110
111 if not 0 <= red <= 255 or not 0 <= green <= 255 or not 0 <= blue <= 255:
112 raise OutOfRangeException("Color values must be between 0 and 255.")
113
114 red = red * ((1 << num_bits_per_pixel) - 1) // 255
115 green = green * ((1 << num_bits_per_pixel) - 1) // 255
116 blue = blue * ((1 << num_bits_per_pixel) - 1) // 255
117
118 color_code = (red << 32) + (green << 16) + blue
119
120 return Color(_color_code=color_code)
121
122 @classmethod
123 def create_from_constant(cls, color_constant: ColorConstant, num_bits_per_pixel: int) -> Color:
124 """!
125 @brief
126 Creates a color from a color constant value.
127
128 @param color_constant: A ColorConstant.
129 @param num_bits_per_pixel: Number of bits per pixel.
130
131 @exception NotSupportedException: If the color constant is unknown.
132
133 @return A Color object.
134
135 @since ids_peak_icv 1.0
136 """
137
138 if color_constant == ColorConstant.BLACK:
139 return Color.create_from_rgb(0, 0, 0, num_bits_per_pixel)
140 elif color_constant == ColorConstant.WHITE:
141 return Color.create_from_rgb(255, 255, 255, num_bits_per_pixel)
142 elif color_constant == ColorConstant.RED:
143 return Color.create_from_rgb(255, 0, 0, num_bits_per_pixel)
144 elif color_constant == ColorConstant.GREEN:
145 return Color.create_from_rgb(0, 255, 0, num_bits_per_pixel)
146 elif color_constant == ColorConstant.BLUE:
147 return Color.create_from_rgb(0, 0, 255, num_bits_per_pixel)
148 elif color_constant == ColorConstant.DIM_GRAY:
149 return Color.create_from_rgb(105, 105, 105, num_bits_per_pixel)
150 elif color_constant == ColorConstant.GRAY:
151 return Color.create_from_rgb(128, 128, 128, num_bits_per_pixel)
152 elif color_constant == ColorConstant.LIGHT_GRAY:
153 return Color.create_from_rgb(211, 211, 211, num_bits_per_pixel)
154 elif color_constant == ColorConstant.CYAN:
155 return Color.create_from_rgb(0, 255, 255, num_bits_per_pixel)
156 elif color_constant == ColorConstant.MAGENTA:
157 return Color.create_from_rgb(255, 0, 255, num_bits_per_pixel)
158 elif color_constant == ColorConstant.YELLOW:
159 return Color.create_from_rgb(255, 255, 0, num_bits_per_pixel)
160 elif color_constant == ColorConstant.MEDIUM_SLATE_BLUE:
161 return Color.create_from_rgb(123, 104, 238, num_bits_per_pixel)
162 elif color_constant == ColorConstant.CORAL:
163 return Color.create_from_rgb(255, 127, 80, num_bits_per_pixel)
164 elif color_constant == ColorConstant.SLATE_BLUE:
165 return Color.create_from_rgb(106, 90, 205, num_bits_per_pixel)
166 elif color_constant == ColorConstant.SPRING_GREEN:
167 return Color.create_from_rgb(0, 255, 127, num_bits_per_pixel)
168 elif color_constant == ColorConstant.ORANGE_RED:
169 return Color.create_from_rgb(255, 69, 0, num_bits_per_pixel)
170 elif color_constant == ColorConstant.DARK_OLIVE_GREEN:
171 return Color.create_from_rgb(85, 107, 47, num_bits_per_pixel)
172 elif color_constant == ColorConstant.PINK:
173 return Color.create_from_rgb(255, 192, 203, num_bits_per_pixel)
174 elif color_constant == ColorConstant.CADET_BLUE:
175 return Color.create_from_rgb(95, 158, 160, num_bits_per_pixel)
176 elif color_constant == ColorConstant.GOLDENROD:
177 return Color.create_from_rgb(218, 165, 32, num_bits_per_pixel)
178 elif color_constant == ColorConstant.ORANGE:
179 return Color.create_from_rgb(255, 165, 0, num_bits_per_pixel)
180 elif color_constant == ColorConstant.GOLD:
181 return Color.create_from_rgb(255, 215, 0, num_bits_per_pixel)
182 elif color_constant == ColorConstant.FOREST_GREEN:
183 return Color.create_from_rgb(34, 139, 34, num_bits_per_pixel)
184 elif color_constant == ColorConstant.CORNFLOWER_BLUE:
185 return Color.create_from_rgb(100, 149, 237, num_bits_per_pixel)
186 elif color_constant == ColorConstant.NAVY:
187 return Color.create_from_rgb(0, 0, 128, num_bits_per_pixel)
188 elif color_constant == ColorConstant.TURQUOISE:
189 return Color.create_from_rgb(64, 224, 208, num_bits_per_pixel)
190 elif color_constant == ColorConstant.DARK_SLATE_BLUE:
191 return Color.create_from_rgb(72, 61, 139, num_bits_per_pixel)
192 elif color_constant == ColorConstant.LIGHT_BLUE:
193 return Color.create_from_rgb(173, 216, 230, num_bits_per_pixel)
194 elif color_constant == ColorConstant.INDIAN_RED:
195 return Color.create_from_rgb(205, 92, 92, num_bits_per_pixel)
196 elif color_constant == ColorConstant.VIOLET_RED:
197 return Color.create_from_rgb(208, 32, 144, num_bits_per_pixel)
198 elif color_constant == ColorConstant.LIGHT_STEEL_BLUE:
199 return Color.create_from_rgb(176, 196, 222, num_bits_per_pixel)
200 elif color_constant == ColorConstant.MEDIUM_BLUE:
201 return Color.create_from_rgb(0, 0, 205, num_bits_per_pixel)
202 elif color_constant == ColorConstant.KHAKI:
203 return Color.create_from_rgb(240, 230, 140, num_bits_per_pixel)
204 elif color_constant == ColorConstant.VIOLET:
205 return Color.create_from_rgb(238, 130, 238, num_bits_per_pixel)
206 elif color_constant == ColorConstant.FIREBRICK:
207 return Color.create_from_rgb(178, 34, 34, num_bits_per_pixel)
208 elif color_constant == ColorConstant.MIDNIGHT_BLUE:
209 return Color.create_from_rgb(25, 25, 112, num_bits_per_pixel)
210 else:
211 raise NotSupportedException("Invalid color constant given.")
212
213 def __init__(self, *args: Any, **kwargs: Any) -> None:
214 """!
215 @brief
216 In order to create a color, please refer to the class methods.
217
218 @exception NotPossibleException: If the class cannot be initialized by default init.
219
220 @since ids_peak_icv 1.0
221 """
222 try:
223 self._color_code: int = kwargs["_color_code"]
224 except KeyError:
226 "Cannot initialize Color with arguments. Please use a class method.")
227
228 @property
229 def __red(self) -> int:
230 """!
231 @brief
232 Get the red value.
233
234 @return The red value.
235
236 @since ids_peak_icv 1.0
237 """
238 return (self._color_code >> 32) & 0xFFFF
239
240 @property
241 def __green(self) -> int:
242 """!
243 @brief
244 Get the green value.
245
246 @return The green value.
247
248 @since ids_peak_icv 1.0
249 """
250 return (self._color_code >> 16) & 0xFFFF
251
252 @property
253 def __blue(self) -> int:
254 """!
255 @brief
256 Get the blue value.
257
258 @return The blue value.
259
260 @since ids_peak_icv 1.0
261 """
262 return self._color_code & 0xFFFF
263
264 @property
265 def rgb(self) -> tuple[int, int, int]:
266 """!
267 @brief
268 The color as a tuple of red, green and blue values.
269
270 @return Tuple with format (r, g, b)
271
272 @since ids_peak_icv 1.0
273 """
274 return self.__red__red, self.__green__green, self.__blue
275
276 @property
277 def color_code(self) -> int:
278 """!
279 @brief
280 Get the color code.
281
282 @return The color as an integer.
283
284 @since ids_peak_icv 1.0
285 """
286 return self._color_code
287
288 def __eq__(self, other: object) -> bool:
289 """!
290 @brief
291 Check if two Color objects are equal.
292
293 @param other: (Color): Another Color object.
294
295 @return True if equal, False otherwise.
296
297 @since ids_peak_icv 1.0
298 """
299
300 if not isinstance(other, Color):
301 return False
302
303 return self._color_code == other._color_code
304
305 def __ne__(self, other: object) -> bool:
306 """!
307 @brief
308 Check if two Color objects are not equal.
309
310 @param other: (Color): Another Color object.
311
312 @return True if not equal, False otherwise.
313
314 @since ids_peak_icv 1.0
315 """
316 return not self.__eq__(other)
317
318 def __str__(self) -> str:
319 """!
320 @brief
321 @return String representation of the object.
322
323 @since ids_peak_icv 1.0
324 """
325 return f"Color(r: {self.__red}, g: {self.__green}, b: {self.__blue})"
326
327
329 """!
330 @brief
331 Class holding a color palette (a set of colors).
332
333 @ingroup ids_peak_icv_python_painting
334
335 @since ids_peak_icv 1.0
336 """
337
338 @classmethod
339 def create_from_colors(cls, *args: Color) -> ColorPalette:
340 """!
341 @brief
342 Create a pallet from a number of Colors.
343
344 @param args: Comma separated Colors.
345 @return A ColorPalette object.
346
347 @since ids_peak_icv 1.0
348 """
349 return ColorPalette(_color_palette=args)
350
351 @classmethod
352 def create_from_palette_constant(cls, color_palette_constant: ColorPaletteConstant,
353 num_bits_per_pixel: int = 8) -> ColorPalette:
354 """!
355 @brief
356 Create a pallet with a specific number of colors.
357
358 The colors in the palett are
359
360 - RED, GREEN, BLUE (for COLORED_3)
361 - RED, GREEN, BLUE, YELLOW, MAGENTA, CYAN (for COLORED_6)
362
363 @param color_palette_constant: A ColorPaletteConstant.
364 @param num_bits_per_pixel: Number of bits per pixel. Defaults to 8.
365 @return A ColorPalette object.
366
367 @since ids_peak_icv 1.0
368 """
369 return ColorPalette(
370 _color_palette=ColorPalette.__create_from_color_palette_constant(color_palette_constant,
371 num_bits_per_pixel))
372
373 def __init__(self, *args: Any, **kwargs: Any) -> None:
374 """!
375 @brief
376 In order to create a color palette, please refer to the class methods.
377
378 @exception NotPossibleException: If the class cannot be initialized by default.
379
380 @since ids_peak_icv 1.0
381 """
382
383 try:
384 self._colorPalette = kwargs["_color_palette"]
385 except KeyError:
387 "Cannot initialize ColorPalette with arguments. Please use a class method.")
388
390 Color.__init__(self, _color_code=self._colorPalette[self._colorPaletteIndex]._color_code)
391
392 @property
393 def rgb(self) -> tuple[int, int, int]:
394 """!
395 @brief
396 Get the next color of the palette as a tuple with the red, green and blue values.
397
398 @return Tuple with format (r, g, b).
399
400 @since ids_peak_icv 1.0
401 """
402 rgb_value = super().rgb
403 self.__next_color()
404 return rgb_value
405
406 @property
407 def color_code(self) -> int:
408 """!
409 @brief
410 Get the next color of the palette as an integer.
411
412 @return The color as an integer.
413
414 @since ids_peak_icv 1.0
415 """
416 color_code_value = super().color_code
417 self.__next_color()
418 return color_code_value
419
420 def __next_color(self) -> None:
421 self._colorPaletteIndex = (self._colorPaletteIndex + 1) % (len(self._colorPalette))
423
424 @classmethod
425 def __create_from_color_palette_constant(cls, color_palette_constant: ColorPaletteConstant,
426 num_bits_per_pixel: int) -> list[Color]:
427 if color_palette_constant == ColorPaletteConstant.COLORED_3:
428 return [Color.create_from_constant(ColorConstant.RED, num_bits_per_pixel),
429 Color.create_from_constant(ColorConstant.GREEN, num_bits_per_pixel),
430 Color.create_from_constant(ColorConstant.BLUE, num_bits_per_pixel)]
431 elif color_palette_constant == ColorPaletteConstant.COLORED_6:
432 return [Color.create_from_constant(ColorConstant.RED, num_bits_per_pixel),
433 Color.create_from_constant(ColorConstant.GREEN, num_bits_per_pixel),
434 Color.create_from_constant(ColorConstant.BLUE, num_bits_per_pixel),
435 Color.create_from_constant(ColorConstant.YELLOW, num_bits_per_pixel),
436 Color.create_from_constant(ColorConstant.MAGENTA, num_bits_per_pixel),
437 Color.create_from_constant(ColorConstant.CYAN, num_bits_per_pixel)]
438 return []
Thrown when an operation cannot be performed due to logical constraints.
Definition exceptions.py:85
Thrown when a requested operation is not implemented or supported.
Definition exceptions.py:71
Thrown when a value is outside the allowed range.
Enum class for color constants.
Definition color.py:9
Class holding color codes.
Definition color.py:82
Color create_from_constant(cls, ColorConstant color_constant, int num_bits_per_pixel)
Creates a color from a color constant value.
Definition color.py:123
None __init__(self, *Any args, **Any kwargs)
In order to create a color, please refer to the class methods.
Definition color.py:213
bool __eq__(self, object other)
Check if two Color objects are equal.
Definition color.py:288
int color_code(self)
Get the color code.
Definition color.py:277
tuple[int, int, int] rgb(self)
The color as a tuple of red, green and blue values.
Definition color.py:265
Color create_from_rgb(cls, int red, int green, int blue, int num_bits_per_pixel=8)
Creates a color from red, green and blue values.
Definition color.py:92
bool __ne__(self, object other)
Check if two Color objects are not equal.
Definition color.py:305
Enum class for color palette constants.
Definition color.py:56
Class holding a color palette (a set of colors).
Definition color.py:328
None __init__(self, *Any args, **Any kwargs)
In order to create a color palette, please refer to the class methods.
Definition color.py:373
ColorPalette create_from_colors(cls, *Color args)
Create a pallet from a number of Colors.
Definition color.py:339
int color_code(self)
Get the next color of the palette as an integer.
Definition color.py:407
tuple[int, int, int] rgb(self)
Get the next color of the palette as a tuple with the red, green and blue values.
Definition color.py:393
ColorPalette create_from_palette_constant(cls, ColorPaletteConstant color_palette_constant, int num_bits_per_pixel=8)
Create a pallet with a specific number of colors.
Definition color.py:353