Skip to content

Commit ae9412e

Browse files
Quantise colour before writing (#1247)
closes RaspberryPiFoundation/digital-editor-issues#871 Code used to test - ``` from sense_hat import SenseHat from time import sleep sense = SenseHat() # --- Quantisation helper to match set_pixel --- def quantise_rgb(r, g, b): r = (r >> 3) << 3 g = (g >> 2) << 2 b = (b >> 3) << 3 return (r, g, b) # --- Colours --- colours = [ (255, 255, 255), # White (255, 0, 0), # Red (0, 255, 0), # Green (0, 0, 255), # Blue (245, 245, 245), # WhiteSmoke (220, 220, 220), # Gainsboro (192, 192, 192), # Silver (105, 105, 105), # DimGray (169, 169, 169), # DarkGray (0, 0, 0), # Black (100, 149, 237), # CornflowerBlue (70, 130, 180), # SteelBlue (0, 0, 205), # MediumBlue (25, 25, 112), # MidnightBlue (0, 0, 139), # DarkBlue (0, 191, 255), # DeepSkyBlue (30, 144, 255), # DodgerBlue (135, 206, 250), # LightSkyBlue (0, 255, 255), # Cyan (64, 224, 208), # Turquoise (0, 128, 128), # Teal (143, 188, 143), # DarkSeaGreen (60, 179, 113), # MediumSeaGreen (46, 139, 87), # SeaGreen (0, 255, 127), # SpringGreen (34, 139, 34), # ForestGreen (50, 205, 50), # LimeGreen (154, 205, 50), # YellowGreen (128, 128, 0), # Olive (85, 107, 47), # DarkOliveGreen (240, 230, 140), # Khaki (255, 255, 0), # Yellow (255, 215, 0), # Gold (184, 134, 11), # DarkGoldenrod (139, 69, 19), # SaddleBrown (210, 105, 30), # Chocolate (255, 140, 0), # DarkOrange (255, 69, 0), # OrangeRed (178, 34, 34), # Firebrick (255, 0, 0), # Red (220, 20, 60), # Crimson (255, 192, 203), # Pink (255, 105, 180), # HotPink (255, 20, 147), # DeepPink (199, 21, 133), # MediumVioletRed (153, 50, 204), # DarkOrchid (148, 0, 211), # DarkViolet (138, 43, 226), # BlueViolet (75, 0, 130), # Indigo ] passed_count = 0 failed_count = 0 for c in colours: r, g, b = c sense.clear() sense.set_pixel(3, 3, r, g, b) actual = tuple(sense.get_pixel(3, 3)) expected = quantise_rgb(r, g, b) # match set_pixel quantization if actual == expected: print(f"✅ PASS: Requested {c} -> Quantised {expected} -> Actual {actual}") passed_count += 1 else: print(f"❌ FAIL: Requested {c} -> Quantised {expected} -> Actual {actual}") failed_count += 1 sleep(0.3) sense.clear() print(f"\n🎉 Tests complete! Passed: {passed_count}, Failed: {failed_count}") ```
1 parent ea1a608 commit ae9412e

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## Unreleased
88

9+
### Changed
10+
11+
- Changed `set_pixel` to quantise the colour before writing (#1247)
12+
913
## [0.31.1] - 2025-09-19
1014

1115
### Added

public/shims/sense_hat/sense_hat_blob.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,13 @@ def set_pixel(self, x, y, *args):
10951095
if element > 255 or element < 0:
10961096
raise ValueError('Pixel elements must be between 0 and 255')
10971097

1098+
# 🔹 Quantise the colour before writing
1099+
r, g, b = pixel
1100+
r = (r >> 3) << 3
1101+
g = (g >> 2) << 2
1102+
b = (b >> 3) << 3
1103+
pixel = (r, g, b)
1104+
10981105
map = self._pix_map[self._rotation]
10991106
# Two bytes per pixel in fb memory, 16 bit RGB565
11001107
# Our custom module stores the tuples not the 16bit value

0 commit comments

Comments
 (0)