2010-01-30 15:29:18 +00:00
|
|
|
/* Created By: Virgil Dupras
|
|
|
|
* Created On: 2010-01-30
|
2012-03-15 18:28:40 +00:00
|
|
|
* Copyright 2012 Hardcoded Software (http://www.hardcoded.net)
|
2010-01-31 10:23:23 +00:00
|
|
|
*
|
2010-09-30 10:17:41 +00:00
|
|
|
* This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2010-01-31 10:23:23 +00:00
|
|
|
* which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
* http://www.hardcoded.net/licenses/bsd_license
|
2010-01-30 15:29:18 +00:00
|
|
|
*/
|
2010-02-04 12:13:08 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
2010-01-30 15:50:49 +00:00
|
|
|
|
|
|
|
/* I know that there strtol out there, but it requires a pointer to
|
|
|
|
* a char, which would in turn require me to buffer my chars around,
|
|
|
|
* making the whole process slower.
|
|
|
|
*/
|
2010-01-30 16:19:40 +00:00
|
|
|
static long
|
2010-01-30 15:50:49 +00:00
|
|
|
xchar_to_long(char c)
|
|
|
|
{
|
|
|
|
if ((c >= 48) && (c <= 57)) { /* 0-9 */
|
|
|
|
return c - 48;
|
|
|
|
}
|
|
|
|
else if ((c >= 65) && (c <= 70)) { /* A-F */
|
|
|
|
return c - 55;
|
|
|
|
}
|
|
|
|
else if ((c >= 97) && (c <= 102)) { /* a-f */
|
|
|
|
return c - 87;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-30 15:29:18 +00:00
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
cache_string_to_colors(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
char *s;
|
2010-01-30 16:19:40 +00:00
|
|
|
Py_ssize_t char_count, color_count, i;
|
2010-01-30 15:29:18 +00:00
|
|
|
PyObject *result;
|
2010-01-31 09:12:26 +00:00
|
|
|
|
2010-01-30 15:50:49 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "s#", &s, &char_count)) {
|
2010-01-30 15:29:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-30 16:19:40 +00:00
|
|
|
color_count = (char_count / 6);
|
|
|
|
result = PyList_New(color_count);
|
2010-01-30 15:29:18 +00:00
|
|
|
if (result == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-30 16:19:40 +00:00
|
|
|
for (i=0; i<color_count; i++) {
|
2010-01-30 15:50:49 +00:00
|
|
|
long r, g, b;
|
2010-01-30 16:19:40 +00:00
|
|
|
Py_ssize_t ci;
|
2010-01-30 15:29:18 +00:00
|
|
|
PyObject *color_tuple;
|
|
|
|
|
2010-01-30 16:19:40 +00:00
|
|
|
ci = i * 6;
|
|
|
|
r = (xchar_to_long(s[ci]) << 4) + xchar_to_long(s[ci+1]);
|
|
|
|
g = (xchar_to_long(s[ci+2]) << 4) + xchar_to_long(s[ci+3]);
|
|
|
|
b = (xchar_to_long(s[ci+4]) << 4) + xchar_to_long(s[ci+5]);
|
2010-01-30 15:29:18 +00:00
|
|
|
|
2010-02-04 12:13:08 +00:00
|
|
|
color_tuple = inttuple(3, r, g, b);
|
2010-01-30 16:19:40 +00:00
|
|
|
if (color_tuple == NULL) {
|
2010-01-30 15:29:18 +00:00
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-30 16:19:40 +00:00
|
|
|
PyList_SET_ITEM(result, i, color_tuple);
|
2010-01-30 15:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef CacheMethods[] = {
|
|
|
|
{"string_to_colors", cache_string_to_colors, METH_VARARGS,
|
|
|
|
"Transform the string 's' in a list of 3 sized tuples."},
|
|
|
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2010-08-11 14:39:06 +00:00
|
|
|
static struct PyModuleDef CacheDef = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"_cache",
|
|
|
|
NULL,
|
|
|
|
-1,
|
|
|
|
CacheMethods,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyInit__cache(void)
|
2010-01-30 15:29:18 +00:00
|
|
|
{
|
2010-08-11 14:39:06 +00:00
|
|
|
PyObject *m = PyModule_Create(&CacheDef);
|
|
|
|
if (m == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|