2222
2323extern ImageColor *_imagePtrJpeg;
2424
25- /*
26- static uint32_t pallete[] = {
27- 0x000000ll, 0xFFFFFFll, 0x008000ll, 0x0000FFll, 0xFF0000ll, 0xFFFF00ll,
28- 0xFFAA00ll,
29- };
30- */
3125
3226#if defined(ARDUINO_INKPLATECOLOR)
3327
@@ -44,6 +38,11 @@ static unsigned int width = E_INK_HEIGHT, height = E_INK_WIDTH;
4438static uint32_t pallete[] = {0xFFFFFF , 0x0000000 , 0xFF0000 };
4539static unsigned int width = E_INK_WIDTH, height = E_INK_HEIGHT;
4640
41+ #elif defined(ARDUINO_ESP32S3_DEV)
42+ static uint32_t pallete[] = { 0x424852 , 0xA1A8A8 , 0xB0AB44 , 0x7D4749 , 0x4B689A , 0x516A64 };
43+ // static uint32_t pallete[] = { 0x000000, 0xFFFFFF, 0xFFFF00, 0xFF0000, 0x0000FF, 0x00FF00};
44+ static unsigned int width = E_INK_WIDTH, height = E_INK_HEIGHT;
45+
4746#endif
4847
4948/* *
@@ -56,7 +55,7 @@ static unsigned int width = E_INK_WIDTH, height = E_INK_HEIGHT;
5655 */
5756uint8_t ImageColor::findClosestPalette (int16_t r, int16_t g, int16_t b)
5857{
59- int64_t minDistance = 0x7fffffffffffffff ;
58+ int64_t minDistance = INT64_MAX ;
6059 uint8_t contenderCount = 0 ;
6160 uint8_t contenderList[sizeof pallete / sizeof pallete[0 ]];
6261
@@ -65,7 +64,17 @@ uint8_t ImageColor::findClosestPalette(int16_t r, int16_t g, int16_t b)
6564 int16_t pr = RED8 (pallete[i]);
6665 int16_t pg = GREEN8 (pallete[i]);
6766 int16_t pb = BLUE8 (pallete[i]);
68- int32_t currentDistance = SQR (r - pr) + SQR (g - pg) + SQR (b - pb);
67+
68+ int32_t dr = r - pr;
69+ int32_t dg = g - pg;
70+ int32_t db = b - pb;
71+
72+ // Perceptual weighted RGB distance (Rec.601)
73+ int32_t currentDistance =
74+ 30 * dr * dr
75+ + 59 * dg * dg
76+ + 11 * db * db;
77+
6978 if (currentDistance < minDistance)
7079 {
7180 minDistance = currentDistance;
@@ -74,19 +83,15 @@ uint8_t ImageColor::findClosestPalette(int16_t r, int16_t g, int16_t b)
7483 }
7584 else if (currentDistance == minDistance)
7685 {
77- contenderList[ contenderCount] = i;
78- contenderCount++;
86+ if ( contenderCount < sizeof pallete / sizeof pallete[ 0 ])
87+ contenderList[ contenderCount++] = i ;
7988 }
8089 }
8190
82- // If your project has a good way to seed rand(),
83- // you can use rand() here to improve dithering quality
84- // when using shades that are exactly between palette colors.
85- // return contenderList[contenderCount <= 1 ? 0 : rand() % contenderCount];
86-
8791 return contenderList[0 ];
8892}
8993
94+
9095/* *
9196 * @brief ditherGetPixelBmp finds dithered value for given pixel
9297 *
0 commit comments