Skip to content

Instantly share code, notes, and snippets.

@billykwok
Last active September 21, 2021 07:32
Show Gist options
  • Save billykwok/812d4971eb0ab7c06ea744e3c9b9844b to your computer and use it in GitHub Desktop.
Save billykwok/812d4971eb0ab7c06ea744e3c9b9844b to your computer and use it in GitHub Desktop.
#define BAUD_RATE 9600
#define DELIMITER '\n'
#define LED_RED 9
#define LED_GREEN 10
#define LED_BLUE 11
#define POT_H A2
#define POT_S A1
#define POT_L A0
#define S(o, n) \
rgb[t[int(hsl[0]) / 60 * 3 + o] + o - 2] = (n + hsl[2] - c / 2) * 255;
const int t[] = {2, 2, 2, 3, 1, 2, 3, 3, 0, 4, 2, 0, 4, 1, 1, 2, 3, 1};
float hsl[3];
uint8_t rgb[3];
void setup() {
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
Serial.begin(BAUD_RATE);
}
void loop() {
hsl[0] = (1023 - analogRead(POT_H)) * 359.99f / 1023.0f;
hsl[1] = (1023 - analogRead(POT_S)) / 1023.0f;
hsl[2] = (1023 - analogRead(POT_L)) / 1023.0f;
// From https://codegolf.stackexchange.com/a/150282
float g = 2 * hsl[2] - 1;
float c = (g < 0 ? 1 + g : 1 - g) * hsl[1];
float a = uint8_t(hsl[0]) % 120 / 60.f - 1;
rgb[0] = S(0, c);
rgb[1] = S(1, c * (a < 0 ? 1 + a : 1 - a));
rgb[2] = S(2, 0);
analogWrite(LED_RED, rgb[0]);
analogWrite(LED_GREEN, rgb[1]);
analogWrite(LED_BLUE, rgb[2]);
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment