#include <GyverOLED.h>
GyverOLED<SSD1306_128x64, OLED_NO_BUFFER> oled;
const int pins[] = {A0, A1, A2}; // Array for RGB pins
const int outputPins[] = {3, 5, 6}; // Array for LED pins
float R, G, B; // Valori RGB normalizzati
float X, Y, Z; // Valori XYZ
float L, a, b; // Valori Lab*
void setup() {
for (int pin : pins) {
pinMode(pin, INPUT);
}
for (int pin : outputPins) {
pinMode(pin, OUTPUT);
}
oled.init();
oled.setScale(1);
}
void loop() {
// Lettura dei valori RGB dai pin analogici
int rawR = analogRead(pins[0]);
int rawG = analogRead(pins[1]);
int rawB = analogRead(pins[2]);
// Normalizzazione dei valori RGB (da 0 a 255)
R = rawR / 1023.0 * 255.0;
G = rawG / 1023.0 * 255.0;
B = rawB / 1023.0 * 255.0;
// Led RGB
analogWrite(outputPins[0], R);
analogWrite(outputPins[1], G);
analogWrite(outputPins[2], B);
// Display RBG
oled.setCursor(0, 0);
oled.print("R: "); oled.print(R, 4); oled.print(" ");
oled.setCursor(0, 1);
oled.print("G: "); oled.print(G, 4); oled.print(" ");
oled.setCursor(0, 2);
oled.print("B: "); oled.print(B, 4); oled.print(" ");
// Conversione RGB -> XYZ
R = R / 255.0;
G = G / 255.0;
B = B / 255.0;
// Correzione gamma
R = (R > 0.04045) ? pow((R + 0.055) / 1.055, 2.4) : (R / 12.92);
G = (G > 0.04045) ? pow((G + 0.055) / 1.055, 2.4) : (G / 12.92);
B = (B > 0.04045) ? pow((B + 0.055) / 1.055, 2.4) : (B / 12.92);
R *= 100;
G *= 100;
B *= 100;
// Matrice di trasformazione sRGB -> XYZ
X = R * 0.4124564 + G * 0.3575761 + B * 0.1804375;
Y = R * 0.2126729 + G * 0.7151522 + B * 0.0721750;
Z = R * 0.0193339 + G * 0.1191920 + B * 0.9503041;
// Valori di riferimento del bianco (D65)
float refX = 95.047;
float refY = 100.000;
float refZ = 108.883;
// Conversione XYZ -> Lab*
X /= refX;
Y /= refY;
Z /= refZ;
X = (X > 0.008856) ? pow(X, 1.0 / 3.0) : (7.787 * X + 16.0 / 116.0);
Y = (Y > 0.008856) ? pow(Y, 1.0 / 3.0) : (7.787 * Y + 16.0 / 116.0);
Z = (Z > 0.008856) ? pow(Z, 1.0 / 3.0) : (7.787 * Z + 16.0 / 116.0);
L = (116.0 * Y) - 16.0;
a = 500.0 * (X - Y);
b = 200.0 * (Y - Z);
// Display Lab*
oled.setCursor(0, 4);
oled.print("L: "); oled.print(L, 4); oled.print(" ");
oled.setCursor(0, 5);
oled.print("a: "); oled.print(a, 4); oled.print(" ");
oled.setCursor(0, 6);
oled.print("b: "); oled.print(b, 4); oled.print(" ");
oled.update();
delay(1000);
}