|
cwist
Modern C web framework with libttak runtime
|
Dynamic CSS Synthesis and Color Mathematics Implementation. More...
#include <cwist/core/html/css_composer.h>#include <cwist/core/mem/alloc.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <ctype.h>
Go to the source code of this file.
Macros | |
| #define | MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c))) |
| #define | MAX3(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c))) |
Functions | |
| cwist_color_hsl | cwist_color_rgb_to_hsl (cwist_color_rgb rgb) |
| Convert an RGB color to the HSL color space. | |
| cwist_color_rgb | cwist_color_hsl_to_rgb (cwist_color_hsl hsl) |
| Convert an HSL color to the RGB color space. | |
| cwist_color_rgb | cwist_color_hex_to_rgb (const char *hex) |
| Parse a hexadecimal color string into an RGB color. | |
| void | cwist_color_rgb_to_hex (cwist_color_rgb rgb, char out_hex[8]) |
| Format an RGB color as a lowercase "#rrggbb" hex string. | |
| void | cwist_css_config_init (cwist_css_config *cfg) |
| Initialize a CSS configuration with default values. | |
| cwist_sstring * | cwist_css_generate_variables (const cwist_css_config *cfg) |
| Generate the CSS custom-property block (:root variables) for a config. | |
| cwist_sstring * | cwist_css_generate_utility_classes (const cwist_css_config *cfg) |
| Generate the utility/component CSS classes that reference the theme variables. | |
| cwist_sstring * | cwist_css_generate_stylesheet (const cwist_css_config *cfg) |
| Generate a complete stylesheet (variables plus utility classes). | |
| void | cwist_css_scope_init (cwist_css_scope *scope, const char *component_name) |
| Initialize a CSS scoping context, deriving a deterministic class suffix. | |
| const char * | cwist_css_scope_class (cwist_css_scope *scope, const char *base_class) |
| Map a base class to its scoped class name, marking it as used. | |
| int | cwist_css_scope_add_rule (cwist_css_scope *scope, const char *base_class, const char *declarations) |
| Attach CSS declarations to a scoped class. | |
| cwist_sstring * | cwist_css_scope_generate_stylesheet (const cwist_css_scope *scope) |
| Emit the stylesheet for all used scope entries that have declarations. | |
| void | cwist_css_scope_destroy (cwist_css_scope *scope) |
| Destroy a CSS scoping context and free all owned resources. | |
| cwist_sstring * | cwist_css_minify (const char *css) |
| Minify CSS source by stripping comments and collapsing whitespace. | |
| cwist_sstring * | cwist_css_bundle (const char *const *parts, size_t count, bool minify) |
| Concatenate CSS parts into one bundle, optionally minified. | |
Dynamic CSS Synthesis and Color Mathematics Implementation.
Definition in file css_composer.c.
Definition at line 16 of file css_composer.c.
Definition at line 15 of file css_composer.c.
| cwist_color_rgb cwist_color_hex_to_rgb | ( | const char * | hex | ) |
Parse a hexadecimal color string into an RGB color.
Parses a hex string (e.g., "#FF0000", "FF0000", "#F00", or "F00") into an RGB struct.
| hex | Hex string in "#rrggbb", "rrggbb", "#rgb", or "rgb" form. A leading '#' is optional. |
hex is NULL or malformed (wrong length or non-hex characters). Definition at line 121 of file css_composer.c.
References cwist_color_rgb::b, cwist_color_rgb::g, and cwist_color_rgb::r.
Referenced by cwist_css_config_init().
| cwist_color_rgb cwist_color_hsl_to_rgb | ( | cwist_color_hsl | hsl | ) |
Convert an HSL color to the RGB color space.
Converts an HSL color back to RGB color space.
| hsl | Hue in degrees (0-360), saturation and lightness in 0-1. |
Definition at line 76 of file css_composer.c.
References cwist_color_rgb::b, cwist_color_rgb::g, cwist_color_hsl::h, cwist_color_hsl::l, cwist_color_rgb::r, and cwist_color_hsl::s.
| void cwist_color_rgb_to_hex | ( | cwist_color_rgb | rgb, |
| char | out_hex[8] | ||
| ) |
Format an RGB color as a lowercase "#rrggbb" hex string.
Formats an RGB struct into a 7-character hex string (e.g., "#FFFFFF").
| rgb | RGB components in the range 0-255. |
| out_hex | Output buffer of at least 8 bytes; left untouched if NULL. |
Definition at line 156 of file css_composer.c.
References cwist_color_rgb::b, cwist_color_rgb::g, and cwist_color_rgb::r.
Referenced by cwist_css_generate_variables().
| cwist_color_hsl cwist_color_rgb_to_hsl | ( | cwist_color_rgb | rgb | ) |
Convert an RGB color to the HSL color space.
Converts an RGB color to HSL color space.
| rgb | RGB components in the range 0-255. |
Definition at line 24 of file css_composer.c.
References cwist_color_rgb::b, cwist_color_rgb::g, cwist_color_hsl::h, cwist_color_hsl::l, MAX3, MIN3, cwist_color_rgb::r, and cwist_color_hsl::s.
Concatenate CSS parts into one bundle, optionally minified.
Concatenate stylesheets in order into one bundle, optionally minified.
| parts | Array of CSS text fragments; may be NULL only if count is 0. |
| count | Number of entries in parts. |
| minify | If true, the concatenated result is passed through cwist_css_minify(). |
Definition at line 654 of file css_composer.c.
References count, cwist_css_minify(), cwist_error_dispose(), cwist_sstring_assign(), cwist_sstring_create(), cwist_sstring_destroy(), and cwist_sstring::data.
| void cwist_css_config_init | ( | cwist_css_config * | cfg | ) |
Initialize a CSS configuration with default values.
Initializes a CSS configuration with sensible defaults.
| cfg | Configuration to fill. No-op if NULL. |
Defaults: Tailwind blue primary, Tailwind green secondary, 8px roundness, 4px spacing base, light mode.
Definition at line 168 of file css_composer.c.
References cwist_color_hex_to_rgb(), cwist_css_config::is_dark_mode, cwist_css_config::primary_color, cwist_css_config::roundness_px, cwist_css_config::secondary_color, and cwist_css_config::spacing_base_px.
| cwist_sstring * cwist_css_generate_stylesheet | ( | const cwist_css_config * | cfg | ) |
Generate a complete stylesheet (variables plus utility classes).
High-level helper to generate a complete stylesheet (Variables + Utilities).
| cfg | Theme configuration; must not be NULL. |
cfg is NULL or allocation fails. Caller owns the returned string. Definition at line 294 of file css_composer.c.
References cwist_css_generate_utility_classes(), cwist_css_generate_variables(), cwist_sstring_append(), cwist_sstring_create(), cwist_sstring_destroy(), and cwist_sstring::data.
| cwist_sstring * cwist_css_generate_utility_classes | ( | const cwist_css_config * | cfg | ) |
Generate the utility/component CSS classes that reference the theme variables.
Generates a set of utility classes based on the design system configuration.
| cfg | Theme configuration (currently unused; classes reference CSS variables). |
Definition at line 248 of file css_composer.c.
References cwist_sstring_append(), and cwist_sstring_create().
Referenced by cwist_css_generate_stylesheet().
| cwist_sstring * cwist_css_generate_variables | ( | const cwist_css_config * | cfg | ) |
Generate the CSS custom-property block (:root variables) for a config.
Generates a CSS string containing CSS Custom Properties (:root variables). Extrapolates hover states, active states, and surface colors mathematically.
| cfg | Theme configuration; must not be NULL. |
cfg is NULL or allocation fails. Caller owns the returned string. Definition at line 195 of file css_composer.c.
References cwist_color_rgb_to_hex(), cwist_sstring_append(), cwist_sstring_create(), cwist_css_config::is_dark_mode, cwist_css_config::primary_color, cwist_css_config::roundness_px, cwist_css_config::secondary_color, and cwist_css_config::spacing_base_px.
Referenced by cwist_css_generate_stylesheet().
| cwist_sstring * cwist_css_minify | ( | const char * | css | ) |
Minify CSS source by stripping comments and collapsing whitespace.
Minify a stylesheet without changing what it means.
| css | CSS source text; must not be NULL. |
Preserves license comments (those opening with a '!'), quoted strings, escapes, and unquoted url() arguments verbatim. Whitespace around structural characters and comments is dropped unless removing it would glue two tokens.
Definition at line 547 of file css_composer.c.
References cwist_alloc(), cwist_error_dispose(), cwist_free(), cwist_sstring_assign_len(), cwist_sstring_create(), and cwist_sstring_destroy().
Referenced by cwist_css_bundle().
| int cwist_css_scope_add_rule | ( | cwist_css_scope * | scope, |
| const char * | base_class, | ||
| const char * | declarations | ||
| ) |
Attach CSS declarations to a scoped class.
Attach declarations to a base class, replacing any earlier ones.
| scope | Scope to modify. |
| base_class | Base class name; must be a valid CSS identifier. |
| declarations | Declaration block text (e.g. "color: red;"). Must not contain '{', '}', or '<'. |
| 0 | Declarations stored; any previous declarations are freed. |
| -1 | Invalid argument, unsafe declaration content, or allocation failure. |
Definition at line 444 of file css_composer.c.
References cwist_free(), cwist_strdup(), and cwist_css_scope::suffix.
| const char * cwist_css_scope_class | ( | cwist_css_scope * | scope, |
| const char * | base_class | ||
| ) |
Map a base class to its scoped class name, marking it as used.
Map a base class to its scoped name ("btn" -> "btn-a1b2c3d4").
| scope | Scope to query. |
| base_class | Base class name; must be a valid CSS identifier. |
Definition at line 427 of file css_composer.c.
References cwist_css_scope::suffix.
| void cwist_css_scope_destroy | ( | cwist_css_scope * | scope | ) |
Destroy a CSS scoping context and free all owned resources.
Release everything the scope owns and reset it to an empty state.
| scope | Scope to tear down. No-op if NULL. |
Frees all entry base names, scoped class strings, and declaration copies, then zeroes the scope struct.
Definition at line 496 of file css_composer.c.
References cwist_css_scope::count, cwist_free(), cwist_sstring_destroy(), and cwist_css_scope::entries.
| cwist_sstring * cwist_css_scope_generate_stylesheet | ( | const cwist_css_scope * | scope | ) |
Emit the stylesheet for all used scope entries that have declarations.
Emit one rule per class that is both used and has declarations.
| scope | Scope to render; must not be NULL. |
scope is NULL or allocation fails. Caller owns the returned string. Definition at line 468 of file css_composer.c.
References cwist_css_scope::count, cwist_error_dispose(), cwist_sstring_assign(), cwist_sstring_create(), cwist_sstring_destroy(), and cwist_css_scope::entries.
| void cwist_css_scope_init | ( | cwist_css_scope * | scope, |
| const char * | component_name | ||
| ) |
Initialize a CSS scoping context, deriving a deterministic class suffix.
Initialise a scope for one component.
| scope | Scope to initialize. No-op if NULL. |
| component_name | Name used to derive the suffix; empty string if NULL. |
The suffix is an unseeded FNV-1a 32-bit hash of the component name, so markup and stylesheets generated in different worker processes agree on the same suffix. Call cwist_css_scope_destroy() to release.
Definition at line 402 of file css_composer.c.
References cwist_css_scope::capacity, cwist_css_scope::count, cwist_css_scope::entries, and cwist_css_scope::suffix.