/ examples / displayio_color_picker_simpletest.py
displayio_color_picker_simpletest.py
 1  # SPDX-FileCopyrightText: 2021 Jose David
 2  #
 3  # SPDX-License-Identifier: MIT
 4  """
 5  Creates a single color picker
 6  """
 7  
 8  import time
 9  import board
10  import terminalio
11  from displayio import Group, TileGrid, Bitmap, Palette
12  from adafruit_display_text import bitmap_label
13  import adafruit_touchscreen
14  from adafruit_displayio_color_picker import color_picker
15  
16  
17  display = board.DISPLAY
18  
19  # TouchScreen Configuration
20  ts = adafruit_touchscreen.Touchscreen(
21      board.TOUCH_XL,
22      board.TOUCH_XR,
23      board.TOUCH_YD,
24      board.TOUCH_YU,
25      calibration=((5200, 59000), (5800, 57000)),
26      size=(display.width, display.height),
27  )
28  
29  # Colorwheel Bitmap file
30  filename = "wheel200.bmp"  # You can find this file in the examples directory in the library Github
31  # Change the imagesize_used according to the bitmap file used. Colorwheel are identified
32  # according to the size in pixels
33  imagesize_used = 200
34  my_colorpicker = color_picker.ColorPicker(
35      filename,
36      display.width // 2 - imagesize_used // 2,
37      display.height // 2 - imagesize_used // 2,
38      imagesize_used,
39  )
40  my_group = Group(max_size=4)
41  my_group.append(my_colorpicker)
42  
43  palette = Palette(2)
44  palette[0] = 0x990099
45  palette[1] = 0x00FFFF
46  
47  bitmap = Bitmap(100, 20, 2)
48  color_square = TileGrid(bitmap, pixel_shader=palette, x=display.width - 100, y=10)
49  my_group.append(color_square)
50  # Adding text information
51  text_area = bitmap_label.Label(
52      terminalio.FONT,
53      text="Color",
54      x=display.width - 100,
55      y=35,
56  )
57  my_group.append(text_area)
58  
59  # Add my_group to the display
60  display.show(my_group)
61  
62  p = False
63  # Start the main loop
64  while True:
65      p = ts.touch_point
66      if p:  # Check if colorpicker is selected
67          if my_colorpicker.contains(p):
68              color = my_colorpicker.when_selected(p, display.height)
69              palette[0] = color
70              print(f"Color Selected is: {hex(color)}")
71              text_area.text = str(hex(color))
72              time.sleep(1.5)
73  
74      time.sleep(0.05)  # touch response on PyPortal is more accurate with a small delay