wwaz / colormodel-php
Basic color models and conversions
v1.0.3
2026-04-22 08:40 UTC
Requires
- php: ^8.1 || ^8.2 || ^8.3 || ^8.4 || ^8.5
Requires (Dev)
- php-parallel-lint/php-parallel-lint: ^1.4
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^8.5
README
Convert, manipulate, and generate color schemes across all major color spaces — with a clean, chainable PHP API.
composer require wwaz/colormodel-php
What it does
- 10 color models — HEX, RGB, RGBA, HSB, HSV, HSL, CMYK, CMYKInt, CIELab, CIELCh, XYZ
- Fluent conversions — chain from one color space to another in a single expression
- Color manipulation — rotate hue, mix colors with custom weights
- Color schemes — Complementary, Triadic, Tetradic, Square, Analogous, Tint, Tone, Shade
- Flexible input/output — accept strings, arrays, integers, or named colors like
'red'
Examples
1. Cross-Format Conversion Chain
Start with a HEX color, rotate its hue by 180°, and export as CMYK — perfect for generating print-ready complementary colors from web values:
use wwaz\Colormodel\Model\Hex; $cmyk = (new Hex('#f00')) ->hue(180) ->toCMYK(); echo $cmyk->toString(); // 100,0,0,0 echo $cmyk->toHtml(); // cmyk(100,0,0,0) echo $cmyk->toArray(); // [100, 0, 0, 0]
2. Mixing Colors with Custom Weight
Blend two colors in any ratio. Great for generating palette gradients or brand color variations programmatically:
use wwaz\Colormodel\Model\Hex; $red = new Hex('#ff0000'); $blue = new Hex('#0000ff'); // 50/50 mix → purple echo $red->mix($blue)->toString(); // 800080 // 75/25 mix (more red) → darker pink echo $red->mix($blue, 0.25)->toString(); // BF0040
3. Generate a Full Color Scheme
Feed in any color, get back a complete set of harmonious variations — all returned in the same color model you started with:
use wwaz\Colormodel\Model\RGB; use wwaz\Colormodel\Scheme\Complementary; use wwaz\Colormodel\Scheme\Triadic; use wwaz\Colormodel\Scheme\Analogous; use wwaz\Colormodel\Scheme\Tint; $base = new RGB(255, 0, 0); // red (new Complementary($base))->get(); // ['255,0,0', '0,255,255'] (new Triadic($base))->get(); // red + 2 harmonics (new Analogous($base))->get(); // neighboring hues (new Tint($base))->get(); // lighter variations // Works with CMYK too — output stays in CMYK use wwaz\Colormodel\Model\CMYKInt; $cyan = new CMYKInt(100, 0, 0, 0); (new Complementary($cyan))->get(); // ['100,0,0,0', '0,100,100,0']
Color Models
| Model | Channels | Typical use |
|---|---|---|
| HEX | #RRGGBB | HTML / CSS |
| RGB | 0–255 per channel | Screen / digital |
| RGBA | RGB + alpha (0–1) | CSS with transparency |
| HSB/HSV | Hue 0–360°, Sat 0–100, Bri 0–100 | Color pickers |
| HSL | Hue 0–360°, Sat 0–100, Lig 0–100 | CSS / design tools |
| CMYK | 0–1 float per channel | Print (float) |
| CMYKInt | 0–100 int per channel | Print (integer) |
| CIELab | L 0–100, a, b axes | Perceptual color science |
| CIELCh | L, Chroma, Hue | Smooth color gradients |
| XYZ | Device-independent reference | Color math / ICC |
Development
Setup
composer install
Quality checks
composer check
You can also run the checks individually:
composer test
composer analyse
composer lint
Backward compatibility policy
- Existing public APIs are kept backward-compatible.
- Legacy usage with
new HEX(...)remains supported. - New APIs are added in an additive way to avoid breaking existing integrations.
Contributing
Please see CONTRIBUTING.md for development workflow and pull request expectations.
License
MIT