diff --git a/Sample/Sample.ino b/Sample/Sample.ino index d701685..3e6649e 100644 --- a/Sample/Sample.ino +++ b/Sample/Sample.ino @@ -1,12 +1,24 @@ #include +// RGB565形式の色から輝度を計算する関数 +uint8_t calculateBrightness(uint16_t color) { + // RGB565からRGB888に変換 + uint8_t r = ((color >> 11) & 0x1F) * 255 / 31; // 5bit -> 8bit + uint8_t g = ((color >> 5) & 0x3F) * 255 / 63; // 6bit -> 8bit + uint8_t b = (color & 0x1F) * 255 / 31; // 5bit -> 8bit + + // 輝度計算(人間の視覚に基づく重み付け) + return (uint8_t)(0.299 * r + 0.587 * g + 0.114 * b); +} + void changeBackgroundColor() { // ランダムな背景色を生成 uint16_t randomColor = random(0x0000, 0xFFFF); // RGB565形式でランダム色生成 M5.Display.fillScreen(randomColor); - // 背景色に対してコントラストの良い文字色を設定 - uint16_t textColor = (randomColor < 0x6000) ? WHITE : BLACK; + // 輝度に基づいて適切な文字色を選択 + uint8_t brightness = calculateBrightness(randomColor); + uint16_t textColor = (brightness < 128) ? WHITE : BLACK; // 輝度128を境界とする M5.Display.setTextColor(textColor); // テキストを再描画