diff --git a/kitty/colors.c b/kitty/colors.c new file mode 100644 index 000000000..2ce58d13d --- /dev/null +++ b/kitty/colors.c @@ -0,0 +1,51 @@ +/* + * colors.c + * Copyright (C) 2016 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#include "data-types.h" + +static uint32_t FG_BG_256[255] = { + 0x000000, // 0 + 0xcd0000, // 1 + 0x00cd00, // 2 + 0xcdcd00, // 3 + 0x0000ee, // 4 + 0xcd00cd, // 5 + 0x00cdcd, // 6 + 0xe5e5e5, // 7 + 0x7f7f7f, // 8 + 0xff0000, // 9 + 0x00ff00, // 10 + 0xffff00, // 11 + 0x5c5cff, // 12 + 0xff00ff, // 13 + 0x00ffff, // 14 + 0xffffff, // 15 +}; + +PyObject* create_256_color_table() { + // colors 16..232: the 6x6x6 color cube + const uint8_t valuerange[6] = {0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff}; + uint8_t i, j=16; + for(i = 0; i < 217; i++, j++) { + uint8_t r = valuerange[(i / 36) % 6], g = valuerange[(i / 6) % 6], b = valuerange[i % 6]; + FG_BG_256[j] = (r << 16) | (g << 8) | b; + } + // colors 233..253: grayscale + for(i = 1; i < 22; i++, j++) { + uint8_t v = 8 + i * 10; + FG_BG_256[j] = (v << 16) | (v << 8) | v; + } + + PyObject *ans = PyTuple_New(255); + if (ans == NULL) return PyErr_NoMemory(); + for (i=0; i < 255; i++) { + PyObject *temp = PyLong_FromUnsignedLong(FG_BG_256[i]); + if (temp == NULL) { Py_CLEAR(ans); return NULL; } + PyTuple_SET_ITEM(ans, i, temp); + } + return ans; +} diff --git a/kitty/data-types.c b/kitty/data-types.c index 0e9cbd4c6..9d4422cd7 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -9,6 +9,7 @@ extern int init_LineBuf(PyObject *); extern int init_Cursor(PyObject *); extern int init_Line(PyObject *); +extern PyObject* create_256_color_table(); #include "gl.h" static PyMethodDef module_methods[] = { @@ -37,6 +38,7 @@ PyInit_fast_data_types(void) { if (!init_Line(m)) return NULL; if (!init_Cursor(m)) return NULL; if (!add_module_gl_constants(m)) return NULL; + if (PyModule_AddObject(m, "FG_BG_256", create_256_color_table()) != 0) return NULL; PyModule_AddIntConstant(m, "BOLD", BOLD_SHIFT); PyModule_AddIntConstant(m, "ITALIC", ITALIC_SHIFT); PyModule_AddIntConstant(m, "REVERSE", REVERSE_SHIFT); diff --git a/setup.py b/setup.py index 57008bf98..2ea6d583f 100644 --- a/setup.py +++ b/setup.py @@ -78,13 +78,14 @@ def option_parser(): help='Build extension modules with debugging symbols') return p + def main(): if sys.version_info < (3, 5): raise SystemExit('python >= 3.5 required') args = option_parser().parse_args() init_env(args.debug) if args.action == 'build': - compile_c_extension('kitty/fast_data_types', 'kitty/line.c', 'kitty/data-types.c', 'kitty/line-buf.c', 'kitty/cursor.c') + compile_c_extension('kitty/fast_data_types', 'kitty/line.c', 'kitty/data-types.c', 'kitty/line-buf.c', 'kitty/cursor.c', 'kitty/colors.c') elif args.action == 'test': os.execlp(sys.executable, sys.executable, os.path.join(base, 'test.py'))