Skip to content

Commit 9dbd25b

Browse files
authored
Merge pull request #685 from pimoroni/feature/inky73-dither
Inky 7.3: Support dithering of arbitrary colours.
2 parents 63de020 + bfb2f8d commit 9dbd25b

15 files changed

+110
-212
lines changed

examples/interstate75/interstate75_rainbow.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@ int main() {
4242

4343
uint8_t hue_map[hub75.width][3];
4444
for(uint i = 0; i < hub75.width; i++) {
45-
uint8_t r=0;
46-
uint8_t g=0;
47-
uint8_t b=0;
48-
graphics.from_hsv(i / (float) hub75.width, 1.0f, 0.7f, r, g, b);
49-
hue_map[i][0] = r;
50-
hue_map[i][1] = g;
51-
hue_map[i][2] = b;
45+
RGB p = RGB::from_hsv(i / (float) hub75.width, 1.0f, 0.7f);
46+
hue_map[i][0] = p.r;
47+
hue_map[i][1] = p.g;
48+
hue_map[i][2] = p.b;
5249
}
5350

5451
hub75.start(dma_complete);

examples/pico_display_2/pico_display_2_demo.cpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,6 @@ Button button_b(PicoDisplay2::B);
2121
Button button_x(PicoDisplay2::X);
2222
Button button_y(PicoDisplay2::Y);
2323

24-
// HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel
25-
// Outputs are rgb in the range 0-255 for each channel
26-
void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) {
27-
float i = floor(h * 6.0f);
28-
float f = h * 6.0f - i;
29-
v *= 255.0f;
30-
uint8_t p = v * (1.0f - s);
31-
uint8_t q = v * (1.0f - f * s);
32-
uint8_t t = v * (1.0f - (1.0f - f) * s);
33-
34-
switch (int(i) % 6) {
35-
case 0: r = v; g = t; b = p; break;
36-
case 1: r = q; g = v; b = p; break;
37-
case 2: r = p; g = v; b = t; break;
38-
case 3: r = p; g = q; b = v; break;
39-
case 4: r = t; g = p; b = v; break;
40-
case 5: r = v; g = p; b = q; break;
41-
}
42-
}
43-
4424
int main() {
4525
st7789.set_backlight(255);
4626

@@ -107,10 +87,10 @@ int main() {
10787

10888
// Since HSV takes a float from 0.0 to 1.0 indicating hue,
10989
// then we can divide millis by the number of milliseconds
110-
// we want a full colour cycle to take. 5000 = 5 sec.
111-
uint8_t r = 0, g = 0, b = 0;
112-
from_hsv((float)millis() / 5000.0f, 1.0f, 0.5f + sinf(millis() / 100.0f / 3.14159f) * 0.5f, r, g, b);
113-
led.set_rgb(r, g, b);
90+
// we want a full colour cycle to take. 5000 = 5 sec
91+
RGB p = RGB::from_hsv((float)millis() / 5000.0f, 1.0f, 0.5f + sinf(millis() / 100.0f / 3.14159f) * 0.5f);
92+
93+
led.set_rgb(p.r, p.g, p.b);
11494

11595

11696
graphics.set_pen(WHITE);

examples/pico_enc_explorer/pico_enc_explorer.cpp

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,11 @@ static const uint8_t STEPS_PER_REV = 24;
2424
BreakoutEncoder enc;
2525
bool toggle = false;
2626

27-
// HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel
28-
// Outputs are rgb in the range 0-255 for each channel
29-
void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) {
30-
float i = floor(h * 6.0f);
31-
float f = h * 6.0f - i;
32-
v *= 255.0f;
33-
uint8_t p = v * (1.0f - s);
34-
uint8_t q = v * (1.0f - f * s);
35-
uint8_t t = v * (1.0f - (1.0f - f) * s);
36-
37-
switch (int(i) % 6) {
38-
case 0: r = v; g = t; b = p; break;
39-
case 1: r = q; g = v; b = p; break;
40-
case 2: r = p; g = v; b = t; break;
41-
case 3: r = p; g = q; b = v; break;
42-
case 4: r = t; g = p; b = v; break;
43-
case 5: r = v; g = p; b = q; break;
44-
}
45-
}
46-
4727
void count_changed(int16_t count) {
4828
printf("Count: %d\n", count);
4929
float h = (count % STEPS_PER_REV) / (float)STEPS_PER_REV;
50-
uint8_t r, g, b;
51-
from_hsv(h, 1.0f, 1.0f, r, g, b);
52-
enc.set_led(r, g, b);
30+
RGB p = RGB::from_hsv(h, 1.0f, 1.0f);
31+
enc.set_led(p.r, p.g, p.b);
5332

5433
graphics.set_pen(BLACK);
5534
graphics.clear();
@@ -58,7 +37,7 @@ void count_changed(int16_t count) {
5837
graphics.set_pen(RED);
5938
std::ostringstream ss;
6039
ss << "R = ";
61-
ss << (int)r;
40+
ss << (int)(p.r);
6241
std::string s(ss.str());
6342
graphics.text(s, Point(10, 10), 220, 6);
6443
}
@@ -67,7 +46,7 @@ void count_changed(int16_t count) {
6746
graphics.set_pen(GREEN);
6847
std::ostringstream ss;
6948
ss << "G = ";
70-
ss << (int)g;
49+
ss << (int)(p.g);
7150
std::string s(ss.str());
7251
graphics.text(s, Point(10, 70), 220, 6);
7352
}
@@ -76,20 +55,20 @@ void count_changed(int16_t count) {
7655
graphics.set_pen(BLUE);
7756
std::ostringstream ss;
7857
ss << "B = ";
79-
ss << (int)b;
58+
ss << (int)(p.b);
8059
std::string s(ss.str());
8160
graphics.text(s, Point(10, 130), 220, 6);
8261
}
8362

8463
{
8564
// Shouldn't really use create_pen in-line.
8665
// In default (RGB332) palette mode this will lookup the nearest 8-bit colour
87-
graphics.set_pen(graphics.create_pen(r, g, b));
66+
graphics.set_pen(graphics.create_pen(p.r, p.g, p.b));
8867
std::ostringstream ss;
8968
ss << "#";
90-
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)r;
91-
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)g;
92-
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)b;
69+
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)(p.r);
70+
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)(p.g);
71+
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)(p.b);
9372
std::string s(ss.str());
9473
graphics.text(s, Point(10, 190), 220, 5);
9574
}

examples/pico_pot_explorer/pico_pot_explorer.cpp

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@ I2C i2c(PICO_EXPLORER);
2424
BreakoutPotentiometer pot(&i2c);
2525
bool toggle = false;
2626

27-
// HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel
28-
// Outputs are rgb in the range 0-255 for each channel
29-
void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) {
30-
float i = floor(h * 6.0f);
31-
float f = h * 6.0f - i;
32-
v *= 255.0f;
33-
uint8_t p = v * (1.0f - s);
34-
uint8_t q = v * (1.0f - f * s);
35-
uint8_t t = v * (1.0f - (1.0f - f) * s);
36-
37-
switch (int(i) % 6) {
38-
case 0: r = v; g = t; b = p; break;
39-
case 1: r = q; g = v; b = p; break;
40-
case 2: r = p; g = v; b = t; break;
41-
case 3: r = p; g = q; b = v; break;
42-
case 4: r = t; g = p; b = v; break;
43-
case 5: r = v; g = p; b = q; break;
44-
}
45-
}
46-
4727
int main() {
4828
#ifdef PICO_DEFAULT_LED_PIN
4929
gpio_init(PICO_DEFAULT_LED_PIN);
@@ -66,9 +46,8 @@ int main() {
6646
float percent = pot.read();
6747

6848
printf("Percent: %d\n", (int)(percent * 100));
69-
uint8_t r = 0, g = 0, b = 0;
70-
from_hsv(percent, 1.0f, 1.0f, r, g, b);
71-
pot.set_led(r, g, b);
49+
RGB p = RGB::from_hsv(percent, 1.0f, 1.0f);
50+
pot.set_led(p.r, p.g, p.b);
7251

7352
graphics.set_pen(BLACK);
7453
graphics.clear();
@@ -77,7 +56,7 @@ int main() {
7756
graphics.set_pen(RED);
7857
std::ostringstream ss;
7958
ss << "R = ";
80-
ss << (int)r;
59+
ss << (int)(p.r);
8160
std::string s(ss.str());
8261
graphics.text(s, Point(10, 10), 220, 6);
8362
}
@@ -86,7 +65,7 @@ int main() {
8665
graphics.set_pen(GREEN);
8766
std::ostringstream ss;
8867
ss << "G = ";
89-
ss << (int)g;
68+
ss << (int)(p.g);
9069
std::string s(ss.str());
9170
graphics.text(s, Point(10, 70), 220, 6);
9271
}
@@ -95,20 +74,20 @@ int main() {
9574
graphics.set_pen(BLUE);
9675
std::ostringstream ss;
9776
ss << "B = ";
98-
ss << (int)b;
77+
ss << (int)(p.b);
9978
std::string s(ss.str());
10079
graphics.text(s, Point(10, 130), 220, 6);
10180
}
10281

10382
{
10483
// Shouldn't really use create_pen in-line.
10584
// In default (RGB332) palette mode this will lookup the nearest 8-bit colour
106-
graphics.set_pen(graphics.create_pen(r, g, b));
85+
graphics.set_pen(graphics.create_pen(p.r, p.g, p.b));
10786
std::ostringstream ss;
10887
ss << "#";
109-
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)r;
110-
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)g;
111-
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)b;
88+
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)(p.r);
89+
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)(p.g);
90+
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << (int)(p.b);
11291
std::string s(ss.str());
11392
graphics.text(s, Point(10, 190), 220, 5);
11493
}

examples/pico_unicorn/demo.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@ using namespace pimoroni;
99

1010
PicoUnicorn pico_unicorn;
1111

12-
void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) {
13-
float i = floor(h * 6.0f);
14-
float f = h * 6.0f - i;
15-
v *= 255.0f;
16-
uint8_t p = v * (1.0f - s);
17-
uint8_t q = v * (1.0f - f * s);
18-
uint8_t t = v * (1.0f - (1.0f - f) * s);
19-
20-
switch (int(i) % 6) {
21-
case 0: r = v; g = t; b = p; break;
22-
case 1: r = q; g = v; b = p; break;
23-
case 2: r = p; g = v; b = t; break;
24-
case 3: r = p; g = q; b = v; break;
25-
case 4: r = t; g = p; b = v; break;
26-
case 5: r = v; g = p; b = q; break;
27-
}
28-
}
29-
30-
3112
int main() {
3213

3314
pico_unicorn.init();
@@ -46,21 +27,6 @@ int main() {
4627
if(pico_unicorn.is_pressed(pico_unicorn.X)) { x_pressed = true; }
4728
if(pico_unicorn.is_pressed(pico_unicorn.Y)) { y_pressed = true; }
4829

49-
/*
50-
for(uint8_t y = 0; y < 7; y++) {
51-
for(uint8_t x = 0; x < 16; x++) {
52-
uint8_t r, g, b;
53-
float h = float(x) / 63.0f + float(i) / 500.0f;
54-
h = h - float(int(h));
55-
float s = 1.0f;//(sin(float(i) / 200.0f) * 0.5f) + 0.5f;
56-
float v = (float(y) / 8.0f) + 0.05f;
57-
from_hsv(h, s, v, r, g, b);
58-
59-
pico_unicorn.set_pixel(x, y, r, g, b);
60-
j = j + 1;
61-
}
62-
}*/
63-
6430
pico_unicorn.clear();
6531

6632
if(a_pressed & b_pressed & x_pressed & y_pressed) {

examples/tufty2040/tufty2040_drawing.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,6 @@ uint32_t time() {
4343
return to_ms_since_boot(t);
4444
}
4545

46-
// HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel
47-
// Outputs are rgb in the range 0-255 for each channel
48-
void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) {
49-
float i = floor(h * 6.0f);
50-
float f = h * 6.0f - i;
51-
v *= 255.0f;
52-
uint8_t p = v * (1.0f - s);
53-
uint8_t q = v * (1.0f - f * s);
54-
uint8_t t = v * (1.0f - (1.0f - f) * s);
55-
56-
switch (int(i) % 6) {
57-
case 0: r = v; g = t; b = p; break;
58-
case 1: r = q; g = v; b = p; break;
59-
case 2: r = p; g = v; b = t; break;
60-
case 3: r = p; g = q; b = v; break;
61-
case 4: r = t; g = p; b = v; break;
62-
case 5: r = v; g = p; b = q; break;
63-
}
64-
}
65-
6646
int main() {
6747
st7789.set_backlight(255);
6848

libraries/pico_graphics/pico_graphics.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,6 @@ namespace pimoroni {
44

55
const uint8_t dither16_pattern[16] = {0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5};
66

7-
void PicoGraphics::from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) {
8-
float i = floor(h * 6.0f);
9-
float f = h * 6.0f - i;
10-
v *= 255.0f;
11-
uint8_t p = v * (1.0f - s);
12-
uint8_t q = v * (1.0f - f * s);
13-
uint8_t t = v * (1.0f - (1.0f - f) * s);
14-
15-
switch (int(i) % 6) {
16-
case 0: r = v; g = t; b = p; break;
17-
case 1: r = q; g = v; b = p; break;
18-
case 2: r = p; g = v; b = t; break;
19-
case 3: r = p; g = q; b = v; break;
20-
case 4: r = t; g = p; b = v; break;
21-
case 5: r = v; g = p; b = q; break;
22-
}
23-
}
24-
257
int PicoGraphics::update_pen(uint8_t i, uint8_t r, uint8_t g, uint8_t b) {return -1;};
268
int PicoGraphics::reset_pen(uint8_t i) {return -1;};
279
int PicoGraphics::create_pen(uint8_t r, uint8_t g, uint8_t b) {return -1;};

0 commit comments

Comments
 (0)