diff --git a/src_c/color.c b/src_c/color.c index f5eddcc0e5..d2c9a1f887 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -484,20 +484,12 @@ _hextoint(char *hex, Uint8 *val) static tristate _hexcolor(PyObject *color, Uint8 rgba[]) { - size_t len; - tristate rcode = TRISTATE_FAIL; - char *name; - PyObject *ascii = PyUnicode_AsASCIIString(color); - if (ascii == NULL) { - rcode = TRISTATE_ERROR; - goto Fail; - } - name = PyBytes_AsString(ascii); + Py_ssize_t len; + char *name = (char *)PyUnicode_AsUTF8AndSize(color, &len); if (name == NULL) { - goto Fail; + return TRISTATE_ERROR; } - len = strlen(name); /* hex colors can be * #RRGGBB * #RRGGBBAA @@ -505,46 +497,40 @@ _hexcolor(PyObject *color, Uint8 rgba[]) * 0xRRGGBBAA */ if (len < 7) { - goto Fail; + return TRISTATE_FAIL; } if (name[0] == '#') { if (len != 7 && len != 9) - goto Fail; + return TRISTATE_FAIL; if (!_hextoint(name + 1, &rgba[0])) - goto Fail; + return TRISTATE_FAIL; if (!_hextoint(name + 3, &rgba[1])) - goto Fail; + return TRISTATE_FAIL; if (!_hextoint(name + 5, &rgba[2])) - goto Fail; + return TRISTATE_FAIL; rgba[3] = 255; if (len == 9 && !_hextoint(name + 7, &rgba[3])) { - goto Fail; + return TRISTATE_FAIL; } - goto Success; + return TRISTATE_SUCCESS; } else if (name[0] == '0' && name[1] == 'x') { if (len != 8 && len != 10) - goto Fail; + return TRISTATE_FAIL; if (!_hextoint(name + 2, &rgba[0])) - goto Fail; + return TRISTATE_FAIL; if (!_hextoint(name + 4, &rgba[1])) - goto Fail; + return TRISTATE_FAIL; if (!_hextoint(name + 6, &rgba[2])) - goto Fail; + return TRISTATE_FAIL; rgba[3] = 255; if (len == 10 && !_hextoint(name + 8, &rgba[3])) { - goto Fail; + return TRISTATE_FAIL; } - goto Success; + return TRISTATE_SUCCESS; } - goto Fail; - -Success: - rcode = TRISTATE_SUCCESS; -Fail: - Py_XDECREF(ascii); - return rcode; + return TRISTATE_FAIL; } static int @@ -601,6 +587,17 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba) color = PyDict_GetItem(_COLORDICT, str_obj); // optimize for correct color names if (!color) { + switch (_hexcolor(str_obj, rgba)) { + case TRISTATE_FAIL: + /* Do re-handling of colordict path below */ + break; + case TRISTATE_ERROR: + /* Some python error raised, so forward it */ + return -1; + default: + /* rgba is set, we are done here */ + return 0; + } name1 = PyObject_CallMethod(str_obj, "replace", "(ss)", " ", ""); if (!name1) { return -1; @@ -613,15 +610,8 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba) color = PyDict_GetItem(_COLORDICT, name2); Py_DECREF(name2); if (!color) { - switch (_hexcolor(str_obj, rgba)) { - case TRISTATE_FAIL: - PyErr_SetString(PyExc_ValueError, "invalid color name"); - return -1; - case TRISTATE_ERROR: - return -1; - default: - return 0; - } + PyErr_SetString(PyExc_ValueError, "invalid color name"); + return -1; } }