{"id":31241627,"url":"https://github.com/arraypress/wp-color-utils","last_synced_at":"2025-09-23T00:12:08.269Z","repository":{"id":312920796,"uuid":"1014478809","full_name":"arraypress/wp-color-utils","owner":"arraypress","description":"A lean WordPress library for essential color operations including conversions, adjustments, and accessibility helpers.","archived":false,"fork":false,"pushed_at":"2025-07-05T20:05:50.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-02T21:19:37.284Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arraypress.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-07-05T20:04:07.000Z","updated_at":"2025-07-05T20:05:53.000Z","dependencies_parsed_at":"2025-09-02T21:19:38.523Z","dependency_job_id":"e1edc18f-fef4-41bf-ab69-9bfd1c69d7ee","html_url":"https://github.com/arraypress/wp-color-utils","commit_stats":null,"previous_names":["arraypress/wp-color-utils"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/arraypress/wp-color-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-color-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-color-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-color-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-color-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arraypress","download_url":"https://codeload.github.com/arraypress/wp-color-utils/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arraypress%2Fwp-color-utils/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276493449,"owners_count":25652182,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-22T02:00:08.972Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-09-23T00:12:05.703Z","updated_at":"2025-09-23T00:12:08.251Z","avatar_url":"https://github.com/arraypress.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WordPress Color Utils\n\nA lean WordPress library for essential color operations. Provides practical utilities for color conversions, adjustments, and accessibility helpers with self-contained methods and robust error handling.\n\n## Installation\n\n```bash\ncomposer require arraypress/wp-color-utils\n```\n\n## Quick Start\n\n```php\nuse ArrayPress\\ColorUtils\\Color;\n\n// Convert colors\n$rgb  = Color::hex_to_rgb( '#ff0000' ); // ['red' =\u003e 255, 'green' =\u003e 0, 'blue' =\u003e 0]\n$hex  = Color::rgb_to_hex( 255, 0, 0 ); // \"#ff0000\"\n$rgba = Color::hex_to_rgba( '#ff0000', 0.5 ); // \"rgba(255, 0, 0, 0.50)\"\n\n// Get contrast color for accessibility\n$text_color = Color::get_contrast_color( '#ff0000' ); // \"#ffffff\"\n\n// Adjust brightness\n$lighter = Color::lighten( '#ff0000', 20 ); // \"#ff3333\"\n$darker  = Color::darken( '#ff0000', 20 ); // \"#cc0000\"\n```\n\n## Features\n\n### Core Conversions\n- **Hex ↔ RGB**: Convert between hex codes and RGB values\n- **Hex to RGBA**: Generate CSS rgba() strings with alpha transparency\n- **Color Validation**: Sanitize and validate hex color codes\n\n### Accessibility Helpers\n- **Contrast Colors**: Get optimal black/white text color for any background\n- **Brightness Detection**: Check if colors are dark or light\n\n### Color Adjustments\n- **Lighten/Darken**: Adjust color brightness by percentage\n- **Color Mixing**: Blend two colors together\n- **Random Colors**: Generate random hex colors\n\n### Self-Contained \u0026 Robust\n- Each method handles its own validation\n- Graceful error handling with null returns\n- No external dependencies\n- Support for 3-character hex codes (#f00 → #ff0000)\n\n## API Reference\n\n### Color Conversions\n\n#### `hex_to_rgb(string $hex): ?array`\nConvert hexadecimal color to RGB array.\n\n```php\n$rgb = Color::hex_to_rgb( '#ff0000' );  // ['red' =\u003e 255, 'green' =\u003e 0, 'blue' =\u003e 0]\n$rgb = Color::hex_to_rgb( 'f00' );      // ['red' =\u003e 255, 'green' =\u003e 0, 'blue' =\u003e 0]\n$rgb = Color::hex_to_rgb( 'invalid' );  // null\n```\n\n#### `rgb_to_hex(int $red, int $green, int $blue): string`\nConvert RGB values to hexadecimal color.\n\n```php\n$hex = Color::rgb_to_hex( 255, 0, 0 );     // \"#ff0000\"\n$hex = Color::rgb_to_hex( 128, 128, 128 ); // \"#808080\"\n$hex = Color::rgb_to_hex( 300, - 10, 0 );   // \"#ff0000\" (clamped to 0-255)\n```\n\n#### `hex_to_rgba(string $hex, float $alpha = 1.0): ?string`\nConvert hex color to CSS rgba() string.\n\n```php\n$rgba = Color::hex_to_rgba( '#ff0000', 0.5 );   // \"rgba(255, 0, 0, 0.50)\"\n$rgba = Color::hex_to_rgba( '#0000ff' );        // \"rgba(0, 0, 255, 1.00)\"\n$rgba = Color::hex_to_rgba( 'invalid', 0.5 );   // null\n```\n\n### Accessibility Helpers\n\n#### `get_contrast_color(string $hex_color): string`\nGet optimal text color (black or white) for a background color.\n\n```php\n$text = Color::get_contrast_color( '#ffffff' ); // \"#000000\"\n$text = Color::get_contrast_color( '#000000' ); // \"#ffffff\"\n$text = Color::get_contrast_color( '#ff0000' ); // \"#ffffff\"\n$text = Color::get_contrast_color( '#ffff00' ); // \"#000000\"\n```\n\n#### `is_dark(string $hex_color): bool`\nCheck if a color is considered dark.\n\n```php\n$dark = Color::is_dark( '#000000' ); // true\n$dark = Color::is_dark( '#ffffff' ); // false\n$dark = Color::is_dark( '#ff0000' ); // true\n$dark = Color::is_dark( 'invalid' ); // false\n```\n\n#### `is_light(string $hex_color): bool`\nCheck if a color is considered light.\n\n```php\n$light = Color::is_light( '#ffffff' ); // true\n$light = Color::is_light( '#000000' ); // false\n$light = Color::is_light( '#ffff00' ); // true\n```\n\n### Color Adjustments\n\n#### `lighten(string $hex_color, int $percent = 10): ?string`\nLighten a color by percentage.\n\n```php\n$lighter = Color::lighten( '#ff0000', 20 );  // \"#ff3333\"\n$lighter = Color::lighten( '#000000', 50 );  // \"#808080\"\n$lighter = Color::lighten( 'invalid', 20 );  // null\n```\n\n#### `darken(string $hex_color, int $percent = 10): ?string`\nDarken a color by percentage.\n\n```php\n$darker = Color::darken( '#ff0000', 20 );   // \"#cc0000\"\n$darker = Color::darken( '#ffffff', 50 );   // \"#808080\"\n$darker = Color::darken( 'invalid', 20 );   // null\n```\n\n#### `mix(string $color1, string $color2, float $ratio = 0.5): ?string`\nMix two colors together.\n\n```php\n$mixed = Color::mix( '#ff0000', '#0000ff', 0.5 ); // \"#800080\" (purple)\n$mixed = Color::mix( '#000000', '#ffffff', 0.3 ); // \"#4d4d4d\"\n$mixed = Color::mix( '#ff0000', 'invalid', 0.5 ); // null\n```\n\n### Utilities\n\n#### `sanitize_hex(string $hex_color): ?string`\nClean and validate hex color codes.\n\n```php\n$clean = Color::sanitize_hex( 'ff0000' );   // \"#ff0000\"\n$clean = Color::sanitize_hex( '#FF0000' );  // \"#ff0000\"\n$clean = Color::sanitize_hex( 'f00' );      // \"#ff0000\"\n$clean = Color::sanitize_hex( 'invalid' );  // null\n```\n\n#### `random(): string`\nGenerate a random hex color.\n\n```php\n$random = Color::random(); // \"#a3b2c1\" (example)\n```\n\n## Use Cases\n\n### Theme Customization\n```php\nuse ArrayPress\\ColorUtils\\Color;\n\n// Get user's primary color\n$primary = get_theme_mod( 'primary_color', '#007cba' );\n\n// Generate theme color variations\n$theme_colors = [\n\t'primary'         =\u003e $primary,\n\t'primary_dark'    =\u003e Color::darken( $primary, 15 ),\n\t'primary_light'   =\u003e Color::lighten( $primary, 15 ),\n\t'text_on_primary' =\u003e Color::get_contrast_color( $primary )\n];\n\n// Use in CSS\necho \"\n.button {\n    background: {$theme_colors['primary']};\n    color: {$theme_colors['text_on_primary']};\n}\n.button:hover {\n    background: {$theme_colors['primary_dark']};\n}\n\";\n```\n\n### Dynamic CSS Generation\n```php\n// Generate CSS with transparency\n$accent_color  = '#e74c3c';\n$overlay_color = Color::hex_to_rgba( $accent_color, 0.8 );\n\necho \"\n.hero-overlay {\n    background: {$overlay_color};\n}\n\";\n```\n\n### Form Validation\n```php\n// Sanitize color input\n$user_color = Color::sanitize_hex( $_POST['brand_color'] );\nif ( $user_color ) {\n\tupdate_option( 'site_accent_color', $user_color );\n} else {\n\twp_die( 'Invalid color format' );\n}\n```\n\n### Accessibility Compliance\n```php\n// Ensure readable text color\n$bg_color   = get_option( 'section_background', '#ffffff' );\n$text_color = Color::get_contrast_color( $bg_color );\n\necho \"\u003cdiv style='background: {$bg_color}; color: {$text_color};'\u003e\";\necho \"This text will always be readable!\";\necho \"\u003c/div\u003e\";\n```\n\n### Color Palette Generation\n```php\n// Create a cohesive color scheme\n$base_color = '#3498db';\n\n$palette = [\n\t'primary'   =\u003e $base_color,\n\t'secondary' =\u003e Color::mix( $base_color, '#e74c3c', 0.3 ),\n\t'success'   =\u003e Color::mix( $base_color, '#27ae60', 0.4 ),\n\t'light'     =\u003e Color::lighten( $base_color, 40 ),\n\t'dark'      =\u003e Color::darken( $base_color, 30 )\n];\n```\n\n### WordPress Customizer Integration\n```php\n// In customizer settings\nfunction register_color_controls( $wp_customize ) {\n\t$wp_customize-\u003eadd_setting( 'header_bg_color', [\n\t\t'sanitize_callback' =\u003e function ( $color ) {\n\t\t\treturn Color::sanitize_hex( $color ) ?: '#ffffff';\n\t\t}\n\t] );\n}\n\n// In theme output\nfunction output_dynamic_styles() {\n\t$header_bg   = get_theme_mod( 'header_bg_color', '#ffffff' );\n\t$header_text = Color::get_contrast_color( $header_bg );\n\n\techo \"\u003cstyle\u003e\n    .site-header {\n        background-color: {$header_bg};\n        color: {$header_text};\n    }\n    \u003c/style\u003e\";\n}\n```\n\n## Error Handling\n\nAll methods handle invalid input gracefully:\n\n```php\n// Invalid hex codes return null\n$result = Color::hex_to_rgb( 'invalid' );  // null\n$result = Color::lighten( 'notacolor', 10 ); // null\n\n// RGB values are automatically clamped\n$hex = Color::rgb_to_hex( 300, - 50, 1000 ); // \"#ff00ff\" (clamped to 0-255)\n\n// Alpha values are clamped to 0-1\n$rgba = Color::hex_to_rgba( '#ff0000', 2.5 ); // Uses 1.0 instead\n```\n\n## Requirements\n\n- PHP 7.4+\n- WordPress 5.0+\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the GPL-2.0-or-later License.\n\n## Support\n\n- [Documentation](https://github.com/arraypress/wp-color-utils)\n- [Issue Tracker](https://github.com/arraypress/wp-color-utils/issues)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-color-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farraypress%2Fwp-color-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farraypress%2Fwp-color-utils/lists"}