mirror of
https://github.com/tmux/tmux.git
synced 2025-01-06 15:58:48 +00:00
Much faster (and smaller) method of mapping RGB colour to an xterm(1)
colour, from Avi Halachmi.
This commit is contained in:
parent
aba4438013
commit
d9450bfccd
352
colour.c
352
colour.c
@ -2,6 +2,7 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
|
||||
* Copyright (c) 2016 Avi Halachmi <avihpit@yahoo.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
@ -24,316 +25,65 @@
|
||||
|
||||
#include "tmux.h"
|
||||
|
||||
/*
|
||||
* Colour to string conversion functions. Bit 8 of the colour means it is one
|
||||
* of the 256 colour palette.
|
||||
*/
|
||||
|
||||
struct colour_rgb {
|
||||
u_char i;
|
||||
u_char r;
|
||||
u_char g;
|
||||
u_char b;
|
||||
};
|
||||
|
||||
const struct colour_rgb colour_from_256[] = {
|
||||
{ 0, 0x00, 0x00, 0x00 }, { 1, 0x00, 0x00, 0x5f },
|
||||
{ 2, 0x00, 0x00, 0x87 }, { 3, 0x00, 0x00, 0xaf },
|
||||
{ 4, 0x00, 0x00, 0xd7 }, { 5, 0x00, 0x00, 0xff },
|
||||
{ 6, 0x00, 0x5f, 0x00 }, { 7, 0x00, 0x5f, 0x5f },
|
||||
{ 8, 0x00, 0x5f, 0x87 }, { 9, 0x00, 0x5f, 0xaf },
|
||||
{ 10, 0x00, 0x5f, 0xd7 }, { 11, 0x00, 0x5f, 0xff },
|
||||
{ 12, 0x00, 0x87, 0x00 }, { 13, 0x00, 0x87, 0x5f },
|
||||
{ 14, 0x00, 0x87, 0x87 }, { 15, 0x00, 0x87, 0xaf },
|
||||
{ 16, 0x00, 0x87, 0xd7 }, { 17, 0x00, 0x87, 0xff },
|
||||
{ 18, 0x00, 0xaf, 0x00 }, { 19, 0x00, 0xaf, 0x5f },
|
||||
{ 20, 0x00, 0xaf, 0x87 }, { 21, 0x00, 0xaf, 0xaf },
|
||||
{ 22, 0x00, 0xaf, 0xd7 }, { 23, 0x00, 0xaf, 0xff },
|
||||
{ 24, 0x00, 0xd7, 0x00 }, { 25, 0x00, 0xd7, 0x5f },
|
||||
{ 26, 0x00, 0xd7, 0x87 }, { 27, 0x00, 0xd7, 0xaf },
|
||||
{ 28, 0x00, 0xd7, 0xd7 }, { 29, 0x00, 0xd7, 0xff },
|
||||
{ 30, 0x00, 0xff, 0x00 }, { 31, 0x00, 0xff, 0x5f },
|
||||
{ 32, 0x00, 0xff, 0x87 }, { 33, 0x00, 0xff, 0xaf },
|
||||
{ 34, 0x00, 0xff, 0xd7 }, { 35, 0x00, 0xff, 0xff },
|
||||
{ 36, 0x5f, 0x00, 0x00 }, { 37, 0x5f, 0x00, 0x5f },
|
||||
{ 38, 0x5f, 0x00, 0x87 }, { 39, 0x5f, 0x00, 0xaf },
|
||||
{ 40, 0x5f, 0x00, 0xd7 }, { 41, 0x5f, 0x00, 0xff },
|
||||
{ 42, 0x5f, 0x5f, 0x00 }, { 43, 0x5f, 0x5f, 0x5f },
|
||||
{ 44, 0x5f, 0x5f, 0x87 }, { 45, 0x5f, 0x5f, 0xaf },
|
||||
{ 46, 0x5f, 0x5f, 0xd7 }, { 47, 0x5f, 0x5f, 0xff },
|
||||
{ 48, 0x5f, 0x87, 0x00 }, { 49, 0x5f, 0x87, 0x5f },
|
||||
{ 50, 0x5f, 0x87, 0x87 }, { 51, 0x5f, 0x87, 0xaf },
|
||||
{ 52, 0x5f, 0x87, 0xd7 }, { 53, 0x5f, 0x87, 0xff },
|
||||
{ 54, 0x5f, 0xaf, 0x00 }, { 55, 0x5f, 0xaf, 0x5f },
|
||||
{ 56, 0x5f, 0xaf, 0x87 }, { 57, 0x5f, 0xaf, 0xaf },
|
||||
{ 58, 0x5f, 0xaf, 0xd7 }, { 59, 0x5f, 0xaf, 0xff },
|
||||
{ 60, 0x5f, 0xd7, 0x00 }, { 61, 0x5f, 0xd7, 0x5f },
|
||||
{ 62, 0x5f, 0xd7, 0x87 }, { 63, 0x5f, 0xd7, 0xaf },
|
||||
{ 64, 0x5f, 0xd7, 0xd7 }, { 65, 0x5f, 0xd7, 0xff },
|
||||
{ 66, 0x5f, 0xff, 0x00 }, { 67, 0x5f, 0xff, 0x5f },
|
||||
{ 68, 0x5f, 0xff, 0x87 }, { 69, 0x5f, 0xff, 0xaf },
|
||||
{ 70, 0x5f, 0xff, 0xd7 }, { 71, 0x5f, 0xff, 0xff },
|
||||
{ 72, 0x87, 0x00, 0x00 }, { 73, 0x87, 0x00, 0x5f },
|
||||
{ 74, 0x87, 0x00, 0x87 }, { 75, 0x87, 0x00, 0xaf },
|
||||
{ 76, 0x87, 0x00, 0xd7 }, { 77, 0x87, 0x00, 0xff },
|
||||
{ 78, 0x87, 0x5f, 0x00 }, { 79, 0x87, 0x5f, 0x5f },
|
||||
{ 80, 0x87, 0x5f, 0x87 }, { 81, 0x87, 0x5f, 0xaf },
|
||||
{ 82, 0x87, 0x5f, 0xd7 }, { 83, 0x87, 0x5f, 0xff },
|
||||
{ 84, 0x87, 0x87, 0x00 }, { 85, 0x87, 0x87, 0x5f },
|
||||
{ 86, 0x87, 0x87, 0x87 }, { 87, 0x87, 0x87, 0xaf },
|
||||
{ 88, 0x87, 0x87, 0xd7 }, { 89, 0x87, 0x87, 0xff },
|
||||
{ 90, 0x87, 0xaf, 0x00 }, { 91, 0x87, 0xaf, 0x5f },
|
||||
{ 92, 0x87, 0xaf, 0x87 }, { 93, 0x87, 0xaf, 0xaf },
|
||||
{ 94, 0x87, 0xaf, 0xd7 }, { 95, 0x87, 0xaf, 0xff },
|
||||
{ 96, 0x87, 0xd7, 0x00 }, { 97, 0x87, 0xd7, 0x5f },
|
||||
{ 98, 0x87, 0xd7, 0x87 }, { 99, 0x87, 0xd7, 0xaf },
|
||||
{ 100, 0x87, 0xd7, 0xd7 }, { 101, 0x87, 0xd7, 0xff },
|
||||
{ 102, 0x87, 0xff, 0x00 }, { 103, 0x87, 0xff, 0x5f },
|
||||
{ 104, 0x87, 0xff, 0x87 }, { 105, 0x87, 0xff, 0xaf },
|
||||
{ 106, 0x87, 0xff, 0xd7 }, { 107, 0x87, 0xff, 0xff },
|
||||
{ 108, 0xaf, 0x00, 0x00 }, { 109, 0xaf, 0x00, 0x5f },
|
||||
{ 110, 0xaf, 0x00, 0x87 }, { 111, 0xaf, 0x00, 0xaf },
|
||||
{ 112, 0xaf, 0x00, 0xd7 }, { 113, 0xaf, 0x00, 0xff },
|
||||
{ 114, 0xaf, 0x5f, 0x00 }, { 115, 0xaf, 0x5f, 0x5f },
|
||||
{ 116, 0xaf, 0x5f, 0x87 }, { 117, 0xaf, 0x5f, 0xaf },
|
||||
{ 118, 0xaf, 0x5f, 0xd7 }, { 119, 0xaf, 0x5f, 0xff },
|
||||
{ 120, 0xaf, 0x87, 0x00 }, { 121, 0xaf, 0x87, 0x5f },
|
||||
{ 122, 0xaf, 0x87, 0x87 }, { 123, 0xaf, 0x87, 0xaf },
|
||||
{ 124, 0xaf, 0x87, 0xd7 }, { 125, 0xaf, 0x87, 0xff },
|
||||
{ 126, 0xaf, 0xaf, 0x00 }, { 127, 0xaf, 0xaf, 0x5f },
|
||||
{ 128, 0xaf, 0xaf, 0x87 }, { 129, 0xaf, 0xaf, 0xaf },
|
||||
{ 130, 0xaf, 0xaf, 0xd7 }, { 131, 0xaf, 0xaf, 0xff },
|
||||
{ 132, 0xaf, 0xd7, 0x00 }, { 133, 0xaf, 0xd7, 0x5f },
|
||||
{ 134, 0xaf, 0xd7, 0x87 }, { 135, 0xaf, 0xd7, 0xaf },
|
||||
{ 136, 0xaf, 0xd7, 0xd7 }, { 137, 0xaf, 0xd7, 0xff },
|
||||
{ 138, 0xaf, 0xff, 0x00 }, { 139, 0xaf, 0xff, 0x5f },
|
||||
{ 140, 0xaf, 0xff, 0x87 }, { 141, 0xaf, 0xff, 0xaf },
|
||||
{ 142, 0xaf, 0xff, 0xd7 }, { 143, 0xaf, 0xff, 0xff },
|
||||
{ 144, 0xd7, 0x00, 0x00 }, { 145, 0xd7, 0x00, 0x5f },
|
||||
{ 146, 0xd7, 0x00, 0x87 }, { 147, 0xd7, 0x00, 0xaf },
|
||||
{ 148, 0xd7, 0x00, 0xd7 }, { 149, 0xd7, 0x00, 0xff },
|
||||
{ 150, 0xd7, 0x5f, 0x00 }, { 151, 0xd7, 0x5f, 0x5f },
|
||||
{ 152, 0xd7, 0x5f, 0x87 }, { 153, 0xd7, 0x5f, 0xaf },
|
||||
{ 154, 0xd7, 0x5f, 0xd7 }, { 155, 0xd7, 0x5f, 0xff },
|
||||
{ 156, 0xd7, 0x87, 0x00 }, { 157, 0xd7, 0x87, 0x5f },
|
||||
{ 158, 0xd7, 0x87, 0x87 }, { 159, 0xd7, 0x87, 0xaf },
|
||||
{ 160, 0xd7, 0x87, 0xd7 }, { 161, 0xd7, 0x87, 0xff },
|
||||
{ 162, 0xd7, 0xaf, 0x00 }, { 163, 0xd7, 0xaf, 0x5f },
|
||||
{ 164, 0xd7, 0xaf, 0x87 }, { 165, 0xd7, 0xaf, 0xaf },
|
||||
{ 166, 0xd7, 0xaf, 0xd7 }, { 167, 0xd7, 0xaf, 0xff },
|
||||
{ 168, 0xd7, 0xd7, 0x00 }, { 169, 0xd7, 0xd7, 0x5f },
|
||||
{ 170, 0xd7, 0xd7, 0x87 }, { 171, 0xd7, 0xd7, 0xaf },
|
||||
{ 172, 0xd7, 0xd7, 0xd7 }, { 173, 0xd7, 0xd7, 0xff },
|
||||
{ 174, 0xd7, 0xff, 0x00 }, { 175, 0xd7, 0xff, 0x5f },
|
||||
{ 176, 0xd7, 0xff, 0x87 }, { 177, 0xd7, 0xff, 0xaf },
|
||||
{ 178, 0xd7, 0xff, 0xd7 }, { 179, 0xd7, 0xff, 0xff },
|
||||
{ 180, 0xff, 0x00, 0x00 }, { 181, 0xff, 0x00, 0x5f },
|
||||
{ 182, 0xff, 0x00, 0x87 }, { 183, 0xff, 0x00, 0xaf },
|
||||
{ 184, 0xff, 0x00, 0xd7 }, { 185, 0xff, 0x00, 0xff },
|
||||
{ 186, 0xff, 0x5f, 0x00 }, { 187, 0xff, 0x5f, 0x5f },
|
||||
{ 188, 0xff, 0x5f, 0x87 }, { 189, 0xff, 0x5f, 0xaf },
|
||||
{ 190, 0xff, 0x5f, 0xd7 }, { 191, 0xff, 0x5f, 0xff },
|
||||
{ 192, 0xff, 0x87, 0x00 }, { 193, 0xff, 0x87, 0x5f },
|
||||
{ 194, 0xff, 0x87, 0x87 }, { 195, 0xff, 0x87, 0xaf },
|
||||
{ 196, 0xff, 0x87, 0xd7 }, { 197, 0xff, 0x87, 0xff },
|
||||
{ 198, 0xff, 0xaf, 0x00 }, { 199, 0xff, 0xaf, 0x5f },
|
||||
{ 200, 0xff, 0xaf, 0x87 }, { 201, 0xff, 0xaf, 0xaf },
|
||||
{ 202, 0xff, 0xaf, 0xd7 }, { 203, 0xff, 0xaf, 0xff },
|
||||
{ 204, 0xff, 0xd7, 0x00 }, { 205, 0xff, 0xd7, 0x5f },
|
||||
{ 206, 0xff, 0xd7, 0x87 }, { 207, 0xff, 0xd7, 0xaf },
|
||||
{ 208, 0xff, 0xd7, 0xd7 }, { 209, 0xff, 0xd7, 0xff },
|
||||
{ 210, 0xff, 0xff, 0x00 }, { 211, 0xff, 0xff, 0x5f },
|
||||
{ 212, 0xff, 0xff, 0x87 }, { 213, 0xff, 0xff, 0xaf },
|
||||
{ 214, 0xff, 0xff, 0xd7 }, { 215, 0xff, 0xff, 0xff },
|
||||
{ 216, 0x08, 0x08, 0x08 }, { 217, 0x12, 0x12, 0x12 },
|
||||
{ 218, 0x1c, 0x1c, 0x1c }, { 219, 0x26, 0x26, 0x26 },
|
||||
{ 220, 0x30, 0x30, 0x30 }, { 221, 0x3a, 0x3a, 0x3a },
|
||||
{ 222, 0x44, 0x44, 0x44 }, { 223, 0x4e, 0x4e, 0x4e },
|
||||
{ 224, 0x58, 0x58, 0x58 }, { 225, 0x62, 0x62, 0x62 },
|
||||
{ 226, 0x6c, 0x6c, 0x6c }, { 227, 0x76, 0x76, 0x76 },
|
||||
{ 228, 0x80, 0x80, 0x80 }, { 229, 0x8a, 0x8a, 0x8a },
|
||||
{ 230, 0x94, 0x94, 0x94 }, { 231, 0x9e, 0x9e, 0x9e },
|
||||
{ 232, 0xa8, 0xa8, 0xa8 }, { 233, 0xb2, 0xb2, 0xb2 },
|
||||
{ 234, 0xbc, 0xbc, 0xbc }, { 235, 0xc6, 0xc6, 0xc6 },
|
||||
{ 236, 0xd0, 0xd0, 0xd0 }, { 237, 0xda, 0xda, 0xda },
|
||||
{ 238, 0xe4, 0xe4, 0xe4 }, { 239, 0xee, 0xee, 0xee },
|
||||
};
|
||||
const struct colour_rgb colour_to_256[] = {
|
||||
{ 0, 0x00, 0x00, 0x00 }, { 1, 0x00, 0x00, 0x5f },
|
||||
{ 2, 0x00, 0x00, 0x87 }, { 3, 0x00, 0x00, 0xaf },
|
||||
{ 4, 0x00, 0x00, 0xd7 }, { 5, 0x00, 0x00, 0xff },
|
||||
{ 6, 0x00, 0x5f, 0x00 }, { 7, 0x00, 0x5f, 0x5f },
|
||||
{ 8, 0x00, 0x5f, 0x87 }, { 9, 0x00, 0x5f, 0xaf },
|
||||
{ 10, 0x00, 0x5f, 0xd7 }, { 11, 0x00, 0x5f, 0xff },
|
||||
{ 12, 0x00, 0x87, 0x00 }, { 13, 0x00, 0x87, 0x5f },
|
||||
{ 14, 0x00, 0x87, 0x87 }, { 15, 0x00, 0x87, 0xaf },
|
||||
{ 16, 0x00, 0x87, 0xd7 }, { 17, 0x00, 0x87, 0xff },
|
||||
{ 18, 0x00, 0xaf, 0x00 }, { 19, 0x00, 0xaf, 0x5f },
|
||||
{ 20, 0x00, 0xaf, 0x87 }, { 21, 0x00, 0xaf, 0xaf },
|
||||
{ 22, 0x00, 0xaf, 0xd7 }, { 23, 0x00, 0xaf, 0xff },
|
||||
{ 24, 0x00, 0xd7, 0x00 }, { 25, 0x00, 0xd7, 0x5f },
|
||||
{ 26, 0x00, 0xd7, 0x87 }, { 27, 0x00, 0xd7, 0xaf },
|
||||
{ 28, 0x00, 0xd7, 0xd7 }, { 29, 0x00, 0xd7, 0xff },
|
||||
{ 30, 0x00, 0xff, 0x00 }, { 31, 0x00, 0xff, 0x5f },
|
||||
{ 32, 0x00, 0xff, 0x87 }, { 33, 0x00, 0xff, 0xaf },
|
||||
{ 34, 0x00, 0xff, 0xd7 }, { 35, 0x00, 0xff, 0xff },
|
||||
{ 216, 0x08, 0x08, 0x08 }, { 217, 0x12, 0x12, 0x12 },
|
||||
{ 218, 0x1c, 0x1c, 0x1c }, { 219, 0x26, 0x26, 0x26 },
|
||||
{ 220, 0x30, 0x30, 0x30 }, { 221, 0x3a, 0x3a, 0x3a },
|
||||
{ 222, 0x44, 0x44, 0x44 }, { 223, 0x4e, 0x4e, 0x4e },
|
||||
{ 224, 0x58, 0x58, 0x58 }, { 36, 0x5f, 0x00, 0x00 },
|
||||
{ 37, 0x5f, 0x00, 0x5f }, { 38, 0x5f, 0x00, 0x87 },
|
||||
{ 39, 0x5f, 0x00, 0xaf }, { 40, 0x5f, 0x00, 0xd7 },
|
||||
{ 41, 0x5f, 0x00, 0xff }, { 42, 0x5f, 0x5f, 0x00 },
|
||||
{ 43, 0x5f, 0x5f, 0x5f }, { 44, 0x5f, 0x5f, 0x87 },
|
||||
{ 45, 0x5f, 0x5f, 0xaf }, { 46, 0x5f, 0x5f, 0xd7 },
|
||||
{ 47, 0x5f, 0x5f, 0xff }, { 48, 0x5f, 0x87, 0x00 },
|
||||
{ 49, 0x5f, 0x87, 0x5f }, { 50, 0x5f, 0x87, 0x87 },
|
||||
{ 51, 0x5f, 0x87, 0xaf }, { 52, 0x5f, 0x87, 0xd7 },
|
||||
{ 53, 0x5f, 0x87, 0xff }, { 54, 0x5f, 0xaf, 0x00 },
|
||||
{ 55, 0x5f, 0xaf, 0x5f }, { 56, 0x5f, 0xaf, 0x87 },
|
||||
{ 57, 0x5f, 0xaf, 0xaf }, { 58, 0x5f, 0xaf, 0xd7 },
|
||||
{ 59, 0x5f, 0xaf, 0xff }, { 60, 0x5f, 0xd7, 0x00 },
|
||||
{ 61, 0x5f, 0xd7, 0x5f }, { 62, 0x5f, 0xd7, 0x87 },
|
||||
{ 63, 0x5f, 0xd7, 0xaf }, { 64, 0x5f, 0xd7, 0xd7 },
|
||||
{ 65, 0x5f, 0xd7, 0xff }, { 66, 0x5f, 0xff, 0x00 },
|
||||
{ 67, 0x5f, 0xff, 0x5f }, { 68, 0x5f, 0xff, 0x87 },
|
||||
{ 69, 0x5f, 0xff, 0xaf }, { 70, 0x5f, 0xff, 0xd7 },
|
||||
{ 71, 0x5f, 0xff, 0xff }, { 225, 0x62, 0x62, 0x62 },
|
||||
{ 226, 0x6c, 0x6c, 0x6c }, { 227, 0x76, 0x76, 0x76 },
|
||||
{ 228, 0x80, 0x80, 0x80 }, { 72, 0x87, 0x00, 0x00 },
|
||||
{ 73, 0x87, 0x00, 0x5f }, { 74, 0x87, 0x00, 0x87 },
|
||||
{ 75, 0x87, 0x00, 0xaf }, { 76, 0x87, 0x00, 0xd7 },
|
||||
{ 77, 0x87, 0x00, 0xff }, { 78, 0x87, 0x5f, 0x00 },
|
||||
{ 79, 0x87, 0x5f, 0x5f }, { 80, 0x87, 0x5f, 0x87 },
|
||||
{ 81, 0x87, 0x5f, 0xaf }, { 82, 0x87, 0x5f, 0xd7 },
|
||||
{ 83, 0x87, 0x5f, 0xff }, { 84, 0x87, 0x87, 0x00 },
|
||||
{ 85, 0x87, 0x87, 0x5f }, { 86, 0x87, 0x87, 0x87 },
|
||||
{ 87, 0x87, 0x87, 0xaf }, { 88, 0x87, 0x87, 0xd7 },
|
||||
{ 89, 0x87, 0x87, 0xff }, { 90, 0x87, 0xaf, 0x00 },
|
||||
{ 91, 0x87, 0xaf, 0x5f }, { 92, 0x87, 0xaf, 0x87 },
|
||||
{ 93, 0x87, 0xaf, 0xaf }, { 94, 0x87, 0xaf, 0xd7 },
|
||||
{ 95, 0x87, 0xaf, 0xff }, { 96, 0x87, 0xd7, 0x00 },
|
||||
{ 97, 0x87, 0xd7, 0x5f }, { 98, 0x87, 0xd7, 0x87 },
|
||||
{ 99, 0x87, 0xd7, 0xaf }, { 100, 0x87, 0xd7, 0xd7 },
|
||||
{ 101, 0x87, 0xd7, 0xff }, { 102, 0x87, 0xff, 0x00 },
|
||||
{ 103, 0x87, 0xff, 0x5f }, { 104, 0x87, 0xff, 0x87 },
|
||||
{ 105, 0x87, 0xff, 0xaf }, { 106, 0x87, 0xff, 0xd7 },
|
||||
{ 107, 0x87, 0xff, 0xff }, { 229, 0x8a, 0x8a, 0x8a },
|
||||
{ 230, 0x94, 0x94, 0x94 }, { 231, 0x9e, 0x9e, 0x9e },
|
||||
{ 232, 0xa8, 0xa8, 0xa8 }, { 108, 0xaf, 0x00, 0x00 },
|
||||
{ 109, 0xaf, 0x00, 0x5f }, { 110, 0xaf, 0x00, 0x87 },
|
||||
{ 111, 0xaf, 0x00, 0xaf }, { 112, 0xaf, 0x00, 0xd7 },
|
||||
{ 113, 0xaf, 0x00, 0xff }, { 114, 0xaf, 0x5f, 0x00 },
|
||||
{ 115, 0xaf, 0x5f, 0x5f }, { 116, 0xaf, 0x5f, 0x87 },
|
||||
{ 117, 0xaf, 0x5f, 0xaf }, { 118, 0xaf, 0x5f, 0xd7 },
|
||||
{ 119, 0xaf, 0x5f, 0xff }, { 120, 0xaf, 0x87, 0x00 },
|
||||
{ 121, 0xaf, 0x87, 0x5f }, { 122, 0xaf, 0x87, 0x87 },
|
||||
{ 123, 0xaf, 0x87, 0xaf }, { 124, 0xaf, 0x87, 0xd7 },
|
||||
{ 125, 0xaf, 0x87, 0xff }, { 126, 0xaf, 0xaf, 0x00 },
|
||||
{ 127, 0xaf, 0xaf, 0x5f }, { 128, 0xaf, 0xaf, 0x87 },
|
||||
{ 129, 0xaf, 0xaf, 0xaf }, { 130, 0xaf, 0xaf, 0xd7 },
|
||||
{ 131, 0xaf, 0xaf, 0xff }, { 132, 0xaf, 0xd7, 0x00 },
|
||||
{ 133, 0xaf, 0xd7, 0x5f }, { 134, 0xaf, 0xd7, 0x87 },
|
||||
{ 135, 0xaf, 0xd7, 0xaf }, { 136, 0xaf, 0xd7, 0xd7 },
|
||||
{ 137, 0xaf, 0xd7, 0xff }, { 138, 0xaf, 0xff, 0x00 },
|
||||
{ 139, 0xaf, 0xff, 0x5f }, { 140, 0xaf, 0xff, 0x87 },
|
||||
{ 141, 0xaf, 0xff, 0xaf }, { 142, 0xaf, 0xff, 0xd7 },
|
||||
{ 143, 0xaf, 0xff, 0xff }, { 233, 0xb2, 0xb2, 0xb2 },
|
||||
{ 234, 0xbc, 0xbc, 0xbc }, { 235, 0xc6, 0xc6, 0xc6 },
|
||||
{ 236, 0xd0, 0xd0, 0xd0 }, { 144, 0xd7, 0x00, 0x00 },
|
||||
{ 145, 0xd7, 0x00, 0x5f }, { 146, 0xd7, 0x00, 0x87 },
|
||||
{ 147, 0xd7, 0x00, 0xaf }, { 148, 0xd7, 0x00, 0xd7 },
|
||||
{ 149, 0xd7, 0x00, 0xff }, { 150, 0xd7, 0x5f, 0x00 },
|
||||
{ 151, 0xd7, 0x5f, 0x5f }, { 152, 0xd7, 0x5f, 0x87 },
|
||||
{ 153, 0xd7, 0x5f, 0xaf }, { 154, 0xd7, 0x5f, 0xd7 },
|
||||
{ 155, 0xd7, 0x5f, 0xff }, { 156, 0xd7, 0x87, 0x00 },
|
||||
{ 157, 0xd7, 0x87, 0x5f }, { 158, 0xd7, 0x87, 0x87 },
|
||||
{ 159, 0xd7, 0x87, 0xaf }, { 160, 0xd7, 0x87, 0xd7 },
|
||||
{ 161, 0xd7, 0x87, 0xff }, { 162, 0xd7, 0xaf, 0x00 },
|
||||
{ 163, 0xd7, 0xaf, 0x5f }, { 164, 0xd7, 0xaf, 0x87 },
|
||||
{ 165, 0xd7, 0xaf, 0xaf }, { 166, 0xd7, 0xaf, 0xd7 },
|
||||
{ 167, 0xd7, 0xaf, 0xff }, { 168, 0xd7, 0xd7, 0x00 },
|
||||
{ 169, 0xd7, 0xd7, 0x5f }, { 170, 0xd7, 0xd7, 0x87 },
|
||||
{ 171, 0xd7, 0xd7, 0xaf }, { 172, 0xd7, 0xd7, 0xd7 },
|
||||
{ 173, 0xd7, 0xd7, 0xff }, { 174, 0xd7, 0xff, 0x00 },
|
||||
{ 175, 0xd7, 0xff, 0x5f }, { 176, 0xd7, 0xff, 0x87 },
|
||||
{ 177, 0xd7, 0xff, 0xaf }, { 178, 0xd7, 0xff, 0xd7 },
|
||||
{ 179, 0xd7, 0xff, 0xff }, { 237, 0xda, 0xda, 0xda },
|
||||
{ 238, 0xe4, 0xe4, 0xe4 }, { 239, 0xee, 0xee, 0xee },
|
||||
{ 180, 0xff, 0x00, 0x00 }, { 181, 0xff, 0x00, 0x5f },
|
||||
{ 182, 0xff, 0x00, 0x87 }, { 183, 0xff, 0x00, 0xaf },
|
||||
{ 184, 0xff, 0x00, 0xd7 }, { 185, 0xff, 0x00, 0xff },
|
||||
{ 186, 0xff, 0x5f, 0x00 }, { 187, 0xff, 0x5f, 0x5f },
|
||||
{ 188, 0xff, 0x5f, 0x87 }, { 189, 0xff, 0x5f, 0xaf },
|
||||
{ 190, 0xff, 0x5f, 0xd7 }, { 191, 0xff, 0x5f, 0xff },
|
||||
{ 192, 0xff, 0x87, 0x00 }, { 193, 0xff, 0x87, 0x5f },
|
||||
{ 194, 0xff, 0x87, 0x87 }, { 195, 0xff, 0x87, 0xaf },
|
||||
{ 196, 0xff, 0x87, 0xd7 }, { 197, 0xff, 0x87, 0xff },
|
||||
{ 198, 0xff, 0xaf, 0x00 }, { 199, 0xff, 0xaf, 0x5f },
|
||||
{ 200, 0xff, 0xaf, 0x87 }, { 201, 0xff, 0xaf, 0xaf },
|
||||
{ 202, 0xff, 0xaf, 0xd7 }, { 203, 0xff, 0xaf, 0xff },
|
||||
{ 204, 0xff, 0xd7, 0x00 }, { 205, 0xff, 0xd7, 0x5f },
|
||||
{ 206, 0xff, 0xd7, 0x87 }, { 207, 0xff, 0xd7, 0xaf },
|
||||
{ 208, 0xff, 0xd7, 0xd7 }, { 209, 0xff, 0xd7, 0xff },
|
||||
{ 210, 0xff, 0xff, 0x00 }, { 211, 0xff, 0xff, 0x5f },
|
||||
{ 212, 0xff, 0xff, 0x87 }, { 213, 0xff, 0xff, 0xaf },
|
||||
{ 214, 0xff, 0xff, 0xd7 }, { 215, 0xff, 0xff, 0xff },
|
||||
};
|
||||
|
||||
int colour_cmp_rgb(const void *, const void *);
|
||||
|
||||
/* Compare function for bsearch(). */
|
||||
int
|
||||
colour_cmp_rgb(const void *lhs0, const void *rhs0)
|
||||
static int
|
||||
colour_dist_sq(int R, int G, int B, int r, int g, int b)
|
||||
{
|
||||
const struct colour_rgb *lhs = lhs0, *rhs = rhs0;
|
||||
|
||||
if (lhs->r < rhs->r)
|
||||
return (-1);
|
||||
if (lhs->r > rhs->r)
|
||||
return (1);
|
||||
|
||||
if (lhs->g < rhs->g)
|
||||
return (-1);
|
||||
if (lhs->g > rhs->g)
|
||||
return (1);
|
||||
|
||||
if (lhs->b < rhs->b)
|
||||
return (-1);
|
||||
if (lhs->b > rhs->b)
|
||||
return (1);
|
||||
|
||||
return (0);
|
||||
return ((R - r) * (R - r) + (G - g) * (G - g) + (B - b) * (B - b));
|
||||
}
|
||||
|
||||
/* Work out the nearest colour from the 256 colour set. */
|
||||
static int
|
||||
colour_to_6cube(int v)
|
||||
{
|
||||
if (v < 48)
|
||||
return (0);
|
||||
if (v < 114)
|
||||
return (1);
|
||||
return ((v - 35) / 40);
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert an RGB triplet to the xterm(1) 256 colour palette.
|
||||
*
|
||||
* xterm provides a 6x6x6 colour cube (16 - 231) and 24 greys (232 - 255). We
|
||||
* map our RGB colour to the closest in the cube, also work out the closest
|
||||
* grey, and use the nearest of the two.
|
||||
*
|
||||
* Note that the xterm has much lower resolution for darker colors (they are
|
||||
* not evenly spread out), so our 6 levels are not evenly spread: 0x0, 0x5f
|
||||
* (95), 0x87 (135), 0xaf (175), 0xd7 (215) and 0xff (255). Greys are more
|
||||
* evenly spread (8, 18, 28 ... 238).
|
||||
*/
|
||||
int
|
||||
colour_find_rgb(u_char r, u_char g, u_char b)
|
||||
{
|
||||
struct colour_rgb rgb = { .r = r, .g = g, .b = b }, *found;
|
||||
u_int distance, lowest, colour, i;
|
||||
int dr, dg, db;
|
||||
static const int q2c[6] = { 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff };
|
||||
int qr, qg, qb, cr, cg, cb, d, idx;
|
||||
int grey_avg, grey_idx, grey;
|
||||
|
||||
found = bsearch(&rgb, colour_to_256, nitems(colour_to_256),
|
||||
sizeof colour_to_256[0], colour_cmp_rgb);
|
||||
if (found != NULL)
|
||||
return (16 + found->i);
|
||||
/* Map RGB to 6x6x6 cube. */
|
||||
qr = colour_to_6cube(r); cr = q2c[qr];
|
||||
qg = colour_to_6cube(g); cg = q2c[qg];
|
||||
qb = colour_to_6cube(b); cb = q2c[qb];
|
||||
|
||||
colour = 16;
|
||||
lowest = UINT_MAX;
|
||||
for (i = 0; i < 240; i++) {
|
||||
dr = (int)colour_from_256[i].r - r;
|
||||
dg = (int)colour_from_256[i].g - g;
|
||||
db = (int)colour_from_256[i].b - b;
|
||||
/* If we have hit the colour exactly, return early. */
|
||||
if (cr == r && cg == g && cb == b)
|
||||
return (16 + (36 * qr) + (6 * qg) + qb);
|
||||
|
||||
distance = dr * dr + dg * dg + db * db;
|
||||
if (distance < lowest) {
|
||||
lowest = distance;
|
||||
colour = 16 + i;
|
||||
}
|
||||
}
|
||||
return (colour);
|
||||
/* Work out the closest grey (average of RGB). */
|
||||
grey_avg = (r + g + b) / 3;
|
||||
if (grey_avg > 238)
|
||||
grey_idx = 23;
|
||||
else
|
||||
grey_idx = (grey_avg - 3) / 10;
|
||||
grey = 8 + (10 * grey_idx);
|
||||
|
||||
/* Is grey or 6x6x6 colour closest? */
|
||||
d = colour_dist_sq(cr, cg, cb, r, g, b);
|
||||
if (colour_dist_sq(grey, grey, grey, r, g, b) < d)
|
||||
idx = 232 + grey_idx;
|
||||
else
|
||||
idx = 16 + (36 * qr) + (6 * qg) + qb;
|
||||
return (idx);
|
||||
}
|
||||
|
||||
/* Set grid cell foreground colour. */
|
||||
|
Loading…
Reference in New Issue
Block a user