Module colorizer.parser.names
This module provides a parser that identifies named colors from a given line of text.
It uses a Trie structure for efficient prefix-based matching of color names to #rrggbb values. The module supports multiple namespaces, enabling flexible configuration and handling of different types of color names (e.g., lowercase, uppercase, camelcase, custom names, Tailwind names).
Namespaces:
- lowercase: Contains color names converted to lowercase (e.g., "red" -> "#ff0000"). - uppercase: Contains color names converted to uppercase (e.g., "RED" -> "#ff0000"). - camelcase: Contains color names in camel case (e.g., "LightBlue" -> "#add8e6"). - tailwind_names: Contains color names based on TailwindCSS conventions, including prefixes. - names_custom: Contains user-defined color names, either as a table or a function returning a table.
The parser dynamically populates the Trie and namespaces based on the provided options. Unused namespaces are left empty, avoiding unnecessary memory usage. Color name matching respects the configured namespaces and user-defined preferences, such as whether to strip digits.
Functions
reset_cache () | Reset the color names cache. |
update_color (name, hex, namespace) | Updates the color value for a given color name. |
parser (line, i, m_opts) | Parses a line to identify color names. |
Functions
- reset_cache ()
- Reset the color names cache. Called from colorizer.setup
- update_color (name, hex, namespace)
-
Updates the color value for a given color name.
Parameters:
- name string: The color name.
- hex string: The color value in hex format.
- namespace string: The color map namespace.
- parser (line, i, m_opts)
-
Parses a line to identify color names.
Parameters:
- line string: The text line to parse.
- i number: The index to start parsing from.
- m_opts table: Matcher opts
Returns:
-
number|nil, string|nil: Length of match and hex value if found.