Skip to content

color_keywords.py

a set of variables and functions to process color keywords and their hexadecimal values.

I'm not a fan of color keyword values due to the inherent limitation in number, but since they are supported by the W3C specifications, I need to have a way to identify whether a color keyword value is valid or not and a way to convert them into their hex format.

get_all_keywords()

returns a list of all of the color keywords.

Returns:

Name Type Description
keywords list

all of the color keywords (basic and extended).

Source code in webcode_tk/color_keywords.py
211
212
213
214
215
216
217
218
def get_all_keywords() -> list:
    """returns a list of all of the color keywords.

    Returns:
        keywords (list): all of the color keywords (basic and
            extended)."""
    keywords = list(get_full_color_keywords().keys())
    return keywords

get_basic_color_keywords()

returns a dictionary of the basic color keywords and their hex equivalent.

Returns:

Name Type Description
basic_color_keywords dict

returns the dictionary of basic color keywords and their hex code equivalent.

Source code in webcode_tk/color_keywords.py
184
185
186
187
188
189
190
191
192
def get_basic_color_keywords() -> dict:
    """returns a dictionary of the basic color keywords and their hex
    equivalent.

    Returns:
        basic_color_keywords: returns the dictionary of basic color
            keywords and their hex code equivalent.
    """
    return basic_color_keywords

get_full_color_keywords()

returns all color keywords (basic and extended) with their hex value.

Returns:

Name Type Description
color_keywords dict

a dictionary with the keyword as the key and the hex code as its value.

Source code in webcode_tk/color_keywords.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def get_full_color_keywords() -> dict:
    """returns all color keywords (basic and extended) with their hex
    value.

    Returns:
        color_keywords: a dictionary with the keyword as the key and
            the hex code as its value."""
    color_keywords = basic_color_keywords
    for i in extended_color_keywords:
        items = i.split(" - ")
        key = items[0]
        val = items[1]
        color_keywords[key] = val
    return color_keywords

get_hex_by_keyword(word)

returns the hex value of a keyword (if valid).

Parameters:

Name Type Description Default
word str

the keyword you are trying to convert to hex.

required

Raises:

Type Description
ValueError

if the keyword is not a valid keyword.

Returns:

Name Type Description
str str

the 6-digit hex code of that value in the format #663399

Source code in webcode_tk/color_keywords.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
def get_hex_by_keyword(word: (str)) -> str:
    """returns the hex value of a keyword (if valid).

    Args:
        word (str): the keyword you are trying to convert to hex.

    Raises:
        ValueError: if the keyword is not a valid keyword.

    Returns:
        str: the 6-digit hex code of that value in the format `#663399`
    """
    word = word.lower()
    if not is_a_keyword(word):
        msg = "That keyword value is not a valid keyword. Did you"
        msg += "misspell the keyword?"
        raise ValueError(msg)
    keywords = get_full_color_keywords()
    hex = keywords.get(word)
    return hex

is_a_keyword(word)

checks the keyword to see if it's a valid color keyword value.

Parameters:

Name Type Description Default
word str

the color keyword in question.

required

Returns:

Name Type Description
is_keyword bool

whether the color value is valid or not.

Source code in webcode_tk/color_keywords.py
221
222
223
224
225
226
227
228
229
230
231
def is_a_keyword(word: str) -> bool:
    """checks the keyword to see if it's a valid color keyword value.

    Args:
        word (str): the color keyword in question.

    Returns:
        is_keyword: whether the color value is valid or not.
    """
    is_keyword = word in get_all_keywords()
    return is_keyword

Notes:

I have added rebeccapurple to the list of extended keywords. This is from CSS Level 4 colors, and I highly recommend you read the article on how that particular color was added to the list of extended keywords below (in references).

References: * CSS3/Color * * Re: [CfC] adding 'rebeccapurple' color to CSS Color Level 4