Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize correct colordict entries #3297

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions src_c/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,36 +598,50 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba)
/* We assume the caller handled this check for us. */
assert(PyUnicode_Check(str_obj));

name1 = PyObject_CallMethod(str_obj, "replace", "(ss)", " ", "");
if (!name1) {
return -1;
}
name2 = PyObject_CallMethod(name1, "lower", NULL);
Py_DECREF(name1);
if (!name2) {
return -1;
}
color = PyDict_GetItem(_COLORDICT, name2);
Py_DECREF(name2);
color = PyDict_GetItem(_COLORDICT,
str_obj); // optimize for correct color names
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:
break;
name1 = PyObject_CallMethod(str_obj, "replace", "(ss)", " ", "");
if (!name1) {
return -1;
}
name2 = PyObject_CallMethod(name1, "lower", NULL);
Py_DECREF(name1);
if (!name2) {
return -1;
}
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:
goto success;
break;
}
}
else {
goto found_color;
}
}
else {
goto found_color;
}
else if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_RESTRICT_SEQ)) {

found_color:
if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_RESTRICT_SEQ)) {
PyErr_Format(PyExc_RuntimeError,
"internal pygame error - colordict is supposed to "
"only have tuple values, but there is an object of "
"type '%s' here - Report this to the pygame devs",
Py_TYPE(color)->tp_name);
return -1;
}
success:
return 0;
}

Expand Down
Loading