… | |
… | |
3365 | unsigned int idx_r = r / (0xff / (Red_levels - 1)); |
3365 | unsigned int idx_r = r / (0xff / (Red_levels - 1)); |
3366 | unsigned int idx_g = g / (0xff / (Green_levels - 1)); |
3366 | unsigned int idx_g = g / (0xff / (Green_levels - 1)); |
3367 | unsigned int idx_b = b / (0xff / (Blue_levels - 1)); |
3367 | unsigned int idx_b = b / (0xff / (Blue_levels - 1)); |
3368 | unsigned int idx = colorcube_index (idx_r, idx_g, idx_b); |
3368 | unsigned int idx = colorcube_index (idx_r, idx_g, idx_b); |
3369 | |
3369 | |
3370 | // minor issue: could update idx 0 few more times |
|
|
3371 | if (rgb24_seqno[idx] == 0 |
|
|
3372 | && rgb24_color[idx] == 0) |
|
|
3373 | goto update; |
|
|
3374 | |
|
|
3375 | if (rgb24_color[idx] == color) |
3370 | if (rgb24_color[idx] == color) |
3376 | return idx + minTermCOLOR24; |
3371 | return idx + minTermCOLOR24; |
3377 | |
3372 | |
3378 | for (int i = idx_r - 1; i <= (signed) idx_r + 1; i++) |
3373 | /* we allow one of the 6 directly neighbouring colours */ |
|
|
3374 | /* to replace the current color, if they not used recently */ |
|
|
3375 | static const signed char dxyz[][3] = { |
|
|
3376 | 0, 0, 0, |
|
|
3377 | 0, 0, -1, |
|
|
3378 | 0, 0, +1, |
|
|
3379 | 0, -1, 0, |
|
|
3380 | 0, +1, 0, |
|
|
3381 | -1, 0, 0, |
|
|
3382 | +1, 0, 0, |
|
|
3383 | }; |
|
|
3384 | |
|
|
3385 | for (int n = 0; n < ecb_array_length (dxyz); ++n) |
3379 | { |
3386 | { |
|
|
3387 | int i = idx_r + dxyz[n][0]; |
|
|
3388 | int j = idx_r + dxyz[n][1]; |
|
|
3389 | int k = idx_r + dxyz[n][2]; |
|
|
3390 | |
3380 | if (!IN_RANGE_EXC (i, 0, Red_levels)) |
3391 | if (!IN_RANGE_EXC (i, 0, Red_levels)) |
3381 | continue; |
3392 | continue; |
3382 | |
3393 | |
3383 | for (int j = idx_g - 1; j <= (signed) idx_g + 1; j++) |
|
|
3384 | { |
|
|
3385 | if (!IN_RANGE_EXC (j, 0, Green_levels)) |
3394 | if (!IN_RANGE_EXC (j, 0, Green_levels)) |
3386 | continue; |
3395 | continue; |
3387 | |
3396 | |
3388 | for (int k = idx_b - 1; k <= (signed) idx_b + 1; k++) |
|
|
3389 | { |
|
|
3390 | if (!IN_RANGE_EXC (k, 0, Blue_levels)) |
3397 | if (!IN_RANGE_EXC (k, 0, Blue_levels)) |
3391 | continue; |
3398 | continue; |
3392 | |
3399 | |
3393 | unsigned int index = colorcube_index (i, j, k); |
3400 | unsigned int index = colorcube_index (i, j, k); |
3394 | |
3401 | |
3395 | // minor issue: could update index 0 few more times |
3402 | // minor issue: could update index 0 few more times |
3396 | if (rgb24_seqno[index] == 0 |
3403 | if ((rgb24_seqno[index] | rgb24_color[index]) == 0) |
3397 | && rgb24_color[index] == 0) |
3404 | { |
3398 | { |
|
|
3399 | idx = index; |
3405 | idx = index; |
3400 | goto update; |
3406 | goto update; |
3401 | } |
3407 | } |
3402 | |
3408 | |
3403 | if (rgb24_color[index] == color) |
3409 | if (rgb24_color[index] == color) |
3404 | return index + minTermCOLOR24; |
3410 | return index + minTermCOLOR24; |
3405 | |
3411 | |
3406 | // like (rgb24_seqno[idx] > rgb24_seqno[index]) |
3412 | // like (rgb24_seqno[idx] > rgb24_seqno[index]) |
3407 | // but also handles wrap around values good enough |
3413 | // but also handles wrap around values good enough |
3408 | if ((uint16_t) (rgb24_seqno[idx] - rgb24_seqno[index]) < 0x7fff) |
3414 | if ((uint16_t)(rgb24_seqno[idx] - rgb24_seqno[index]) < 0x7fff) |
3409 | idx = index; |
3415 | idx = index; |
3410 | } |
|
|
3411 | } |
|
|
3412 | } |
3416 | } |
3413 | |
3417 | |
3414 | update: |
3418 | update: |
3415 | rgb24_color[idx] = color; |
3419 | rgb24_color[idx] = color; |
3416 | rgb24_seqno[idx] = ++rgb24_sequence; |
3420 | rgb24_seqno[idx] = ++rgb24_sequence; |