IOLink Python 1.11.0
Loading...
Searching...
No Matches
colorWheel.py

This code demonstrates how to draw a color wheel into a RGBA ImageView.

This code demonstrates how to draw a color wheel into a RGBA ImageView. The part outside the wheel is transparent, using alpha channel.

1import iolink
2
3import math
4from array import array
5
6def hsvToRgb(h: float, s: float, v: float) -> iolink.Vector3u8:
7 hp = h / 60.0
8 c = s * v
9 x = c * (1 - abs(math.fmod(hp, 2) - 1))
10 m = v - c
11 r = 0
12 g = 0
13 b = 0
14
15 if hp <= 1:
16 r = c
17 g = x
18 elif hp <= 2:
19 r = x
20 g = c
21 elif hp <= 3:
22 g = c
23 b = x
24 elif hp <= 4:
25 g = x
26 b = c
27 elif hp <= 5:
28 r = x
29 b = c
30 else:
31 r = c
32 b = x
33
34 rgb_double = iolink.Vector3d(r, g, b)
35 rgb_double += iolink.Vector3d.create_uniform(m)
36 rgb_double *= 255.0
37 return iolink.Vector3u8(int(rgb_double[0]), int(rgb_double[1]), int(rgb_double[2]))
38
39
40
43def colorWheel(shape: iolink.VectorXu64):
44
45 # first we can check if given shape has exactly 2 dimensions
46 if shape.size() != 2:
47 raise ValueError("Shape must have 2 dimensions ")
48
49 # To create a RGBA ImageView, the datatype of the image must have 4 components, and so
50 # should be be VEC4_XXX (with XXX: INT8, INT16, INT32, INT64, UINT8, ...)
51 # We choose VEC4_UINT8 as the datatype for this example.
52 dt = iolink.DataTypeId.VEC4_UINT8
53 image = iolink.ImageViewFactory.allocate(shape, dt)
54
55 # indicate the created image is a 2D IMAGE with RGB color interpretation and alpha channel
56 image.axes_interpretation = iolink.AxesInterpretationId.IMAGE
57 image.image_interpretation = iolink.ImageInterpretation.RGB
58 image.has_alpha = True
59
60 center = iolink.Vector2d(shape[0] / 2.0, shape[1] / 2.0)
61
62 for y in range(0, shape[1]):
63 for x in range(0, shape[0]):
64 position = iolink.Vector2d(x, y)
65 delta = position - center
66 r = delta.length()
67
68 if r > center[0]:
69 # write transparent pixel
70 pixVal = array('B', [0, 0, 0, 0])
71 image.write([x, y], pixVal)
72 else:
73 # determine HSV value for current pixel position
74 h = math.atan2(delta[1], delta[0]) * 180 / math.pi + 180
75 s = r / (shape[0] / 2)
76 v = 1
77 # convert HSV to RGB
78 rgb = hsvToRgb(h, s, v)
79 # write RGBA pixel (with alpha = 255)
80 pixVal = array('B', [*rgb, 255])
81 image.write([x, y], pixVal)
82
83 return image
84
85
86image = colorWheel(iolink.VectorXu64([512, 512]))
87
88print("Generated image:", str(image))
89
90
92
93#import numpy as np
94#from matplotlib import pyplot as plt
95
96
98
99#plt.imshow(npy, interpolation='nearest')
100#plt.show()
101
102print("SUCCESS")
Definition colorWheel.py:1