Skip to content

Remove colordict, rework parsing color from string #3461

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion buildconfig/stubs/gen_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"base",
"bufferproxy",
"color",
"colordict",
"mixer_music",
"pixelarray",
"rect",
Expand Down
1 change: 0 additions & 1 deletion buildconfig/stubs/pygame/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ from . import (
base as base,
bufferproxy as bufferproxy,
color as color,
colordict as colordict,
mixer_music as mixer_music,
pixelarray as pixelarray,
rect as rect,
Expand Down
2 changes: 0 additions & 2 deletions buildconfig/stubs/pygame/color.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ from typing import Any, ClassVar, SupportsIndex, Union, overload
from pygame.typing import ColorLike
from typing_extensions import deprecated # added in 3.13

THECOLORS: dict[str, tuple[int, int, int, int]]

# Color confirms to the Collection ABC, since it also confirms to
# Sized, Iterable and Container ABCs
class Color(Collection[int]):
Expand Down
3 changes: 1 addition & 2 deletions docs/reST/ref/color_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,7 @@ Named Colors
</style>

:doc:`color` lets you specify any of these named colors when creating a new
``pygame.Color`` (taken from the
`colordict module <https://github.com/pygame-community/pygame-ce/blob/main/src_py/colordict.py>`_).
``pygame.Color``

.. role:: aliceblue
.. role:: antiquewhite
Expand Down
42 changes: 8 additions & 34 deletions src_c/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ pg_round(double d)

typedef enum { TRISTATE_SUCCESS, TRISTATE_FAIL, TRISTATE_ERROR } tristate;

static PyObject *_COLORDICT = NULL;

static int
_get_double(PyObject *, double *);
static int
Expand Down Expand Up @@ -588,19 +586,18 @@ _color_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject *)_color_new_internal_length(type, DEFAULT_RGBA, 4);
}

#include "colordict.c"

static int
_parse_color_from_text(PyObject *str_obj, Uint8 *rgba)
{
/* Named color */
PyObject *color = NULL;
PyObject *name1 = NULL, *name2 = NULL;

/* We assume the caller handled this check for us. */
assert(PyUnicode_Check(str_obj));

color = PyDict_GetItem(_COLORDICT,
str_obj); // optimize for correct color names
if (!color) {
if (!parse_color_from_colordict(str_obj, &rgba[0], &rgba[1], &rgba[2])) {
switch (_hexcolor(str_obj, rgba)) {
case TRISTATE_FAIL:
/* Do re-handling of colordict path below */
Expand All @@ -621,22 +618,14 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba)
if (!name2) {
return -1;
}
color = PyDict_GetItem(_COLORDICT, name2);
Py_DECREF(name2);
if (!color) {
if (!parse_color_from_colordict(name2, &rgba[0], &rgba[1], &rgba[2])) {
Py_DECREF(name2);
PyErr_SetString(PyExc_ValueError, "invalid color name");
return -1;
}
Py_DECREF(name2);
}

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;
}
rgba[3] = 255;
return 0;
}

Expand Down Expand Up @@ -2478,7 +2467,7 @@ pg_MappedColorFromObj(PyObject *val, SDL_Surface *surf, Uint32 *color,

MODINIT_DEFINE(color)
{
PyObject *module = NULL, *colordict_module, *apiobj;
PyObject *module = NULL, *apiobj;
static void *c_api[PYGAMEAPI_COLOR_NUMSLOTS];

static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
Expand All @@ -2499,17 +2488,6 @@ MODINIT_DEFINE(color)
return NULL;
}

colordict_module = PyImport_ImportModule("pygame.colordict");
if (!colordict_module) {
return NULL;
}

_COLORDICT = PyObject_GetAttrString(colordict_module, "THECOLORS");
Py_DECREF(colordict_module);
if (!_COLORDICT) {
return NULL;
}

/* type preparation */
if (PyType_Ready(&pgColor_Type) < 0) {
goto error;
Expand All @@ -2524,9 +2502,6 @@ MODINIT_DEFINE(color)
if (PyModule_AddObjectRef(module, "Color", (PyObject *)&pgColor_Type)) {
goto error;
}
if (PyModule_AddObjectRef(module, "THECOLORS", _COLORDICT)) {
goto error;
}

c_api[0] = &pgColor_Type;
c_api[1] = pgColor_New;
Expand All @@ -2543,6 +2518,5 @@ MODINIT_DEFINE(color)

error:
Py_XDECREF(module);
Py_DECREF(_COLORDICT);
return NULL;
}
Loading
Loading