Faster color table

This commit is contained in:
Kovid Goyal 2016-11-10 08:00:06 +05:30
parent 6334b39935
commit 32e4de1c79
3 changed files with 55 additions and 1 deletions

51
kitty/colors.c Normal file
View File

@ -0,0 +1,51 @@
/*
* colors.c
* Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
*
* 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;
}

View File

@ -9,6 +9,7 @@
extern int init_LineBuf(PyObject *); extern int init_LineBuf(PyObject *);
extern int init_Cursor(PyObject *); extern int init_Cursor(PyObject *);
extern int init_Line(PyObject *); extern int init_Line(PyObject *);
extern PyObject* create_256_color_table();
#include "gl.h" #include "gl.h"
static PyMethodDef module_methods[] = { static PyMethodDef module_methods[] = {
@ -37,6 +38,7 @@ PyInit_fast_data_types(void) {
if (!init_Line(m)) return NULL; if (!init_Line(m)) return NULL;
if (!init_Cursor(m)) return NULL; if (!init_Cursor(m)) return NULL;
if (!add_module_gl_constants(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, "BOLD", BOLD_SHIFT);
PyModule_AddIntConstant(m, "ITALIC", ITALIC_SHIFT); PyModule_AddIntConstant(m, "ITALIC", ITALIC_SHIFT);
PyModule_AddIntConstant(m, "REVERSE", REVERSE_SHIFT); PyModule_AddIntConstant(m, "REVERSE", REVERSE_SHIFT);

View File

@ -78,13 +78,14 @@ def option_parser():
help='Build extension modules with debugging symbols') help='Build extension modules with debugging symbols')
return p return p
def main(): def main():
if sys.version_info < (3, 5): if sys.version_info < (3, 5):
raise SystemExit('python >= 3.5 required') raise SystemExit('python >= 3.5 required')
args = option_parser().parse_args() args = option_parser().parse_args()
init_env(args.debug) init_env(args.debug)
if args.action == 'build': 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': elif args.action == 'test':
os.execlp(sys.executable, sys.executable, os.path.join(base, 'test.py')) os.execlp(sys.executable, sys.executable, os.path.join(base, 'test.py'))