For some reason, TFT libraries support setting colors given by RGB either in 255, 255, 255 mode or using Color565, where red and blue are given als 5-bit values (0-31) and green as 6-bit value (0-63). To convert a given number to a color with maximum saturation, it has to be converted. This sketch shows you a simple way how this can be done:
#define cs 10
#define dc 9
#define rst 8
#include <TFT.h>
TFT tft = TFT(cs, dc, rst);
void setup() {
tft.begin();
tft.setRotation(3);
tft.fillScreen(0);
for (int i = 0; i < 32; i++) {
int x = (i % 8) * 20; // % 4;
int y = (i / 8) * 32; // i / 4;
int c = hue(i);
tft.fillRect(x, y, 18, 30, c);
tft.setTextColor(0);
tft.setCursor(x+1, y+1);
tft.print(i);
}
}
void loop() {}
word hue(int z) {
int M = 127;
float h = z * M * 6 / 33.0;
int g = max(-abs(h - 2 * M) + 3 * M - M, 0);
int r = max(+abs(h - 3 * M) + 0 * M - M, 0);
int b = max(-abs(h - 4 * M) + 3 * M - M, 0);
return tft.Color565(r, g, b);
}
Uploading this sketch will give you exactly the picture shown above.
You also might try this sketch:
#define cs 10
#define dc 9
#define rst 8
#include <TFT.h>
TFT tft = TFT(cs, dc, rst);
int x = 0;
int y = 0;
void setup() {
tft.begin();
tft.setRotation(3);
tft.fillScreen(0);
tft.setTextSize(2);
for (int i = 0; i < 32; i++) {
int c = hue(i);
tft.setTextColor(c);
tft.setCursor(x, y);
tft.print(i);
if (i < 10)
x = x + 15;
else x = x + 27;
if (x > 140) {
x = 0;
y = y + 24;
}
}
}
void loop() {}
word hue(int z) {
int M = 127;
float h = z * M * 6 / 33.0;
int g = max(-abs(h - 2 * M) + 3 * M - M, 0);
int r = max(+abs(h - 3 * M) + 0 * M - M, 0);
int b = max(-abs(h - 4 * M) + 3 * M - M, 0);
return tft.Color565(r, g, b);
}
Comments