PHPackages                             renfordt/colors - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. renfordt/colors

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

renfordt/colors
===============

A php library to convert and modify colors.

v2.2.0(2w ago)04.9k↑840%[1 PRs](https://codeberg.org/renfordt/colors/pulls)1MITPHPPHP ^8.4CI passing

Since Aug 6Pushed 6mo ago1 watchersCompare

[ Source](https://codeberg.org/renfordt/colors)[ Packagist](https://packagist.org/packages/renfordt/colors)[ RSS](/packages/renfordt-colors/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (6)Dependencies (24)Versions (12)Used By (1)

🎨 renfordt/colors
=================

[](#-renfordtcolors)

[![Badge](https://camo.githubusercontent.com/f7c54ebedae5ab4472618eefcdb51a8a5bc0bd313928254e0d582daa8485a9af/687474703a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d72656e666f7264742f636f6c6f72732d626c75652e737667)](https://github.com/renfordt/colors)[![Packagist Version](https://camo.githubusercontent.com/e9c9f577ab548ee4b57b256f8472ecf7560e0a9765a9956db93face001546f01/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72656e666f7264742f636f6c6f7273)](https://packagist.org/packages/renfordt/colors/)[![Packagist PHP Version](https://camo.githubusercontent.com/19bb0741112e00e430a134af30ce59348564248bff9ed2f4fc1136ceeaae8f82/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f72656e666f7264742f636f6c6f72732f706870)](https://camo.githubusercontent.com/19bb0741112e00e430a134af30ce59348564248bff9ed2f4fc1136ceeaae8f82/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f72656e666f7264742f636f6c6f72732f706870)[![GitHub License](https://camo.githubusercontent.com/914fb3cc12acfcdd472ebf569be206187adce862e7629673fdd8636e87ba7f01/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f72656e666f7264742f636f6c6f7273)](https://camo.githubusercontent.com/914fb3cc12acfcdd472ebf569be206187adce862e7629673fdd8636e87ba7f01/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f72656e666f7264742f636f6c6f7273)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/348d7e82c3dd3d20305b1cd0fcc1470d159e386a6d3ad3dd2155064c484a465a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72656e666f7264742f636f6c6f72732f74657374732e796d6c3f6c6f676f3d676974687562)](https://camo.githubusercontent.com/348d7e82c3dd3d20305b1cd0fcc1470d159e386a6d3ad3dd2155064c484a465a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72656e666f7264742f636f6c6f72732f74657374732e796d6c3f6c6f676f3d676974687562)[![Code Coverage](https://camo.githubusercontent.com/275292072d913089f1e441bdadffbc54ae51c0c0116c360a5e1c84d4ccde2988/68747470733a2f2f716c74792e73682f67682f72656e666f7264742f70726f6a656374732f636f6c6f72732f636f7665726167652e737667)](https://qlty.sh/gh/renfordt/projects/colors)[![Maintainability](https://camo.githubusercontent.com/d48f12048569df03aacf71b30dcad0f01650718cc54fac6520eda2c2e15ce00b/68747470733a2f2f716c74792e73682f67682f72656e666f7264742f70726f6a656374732f636f6c6f72732f6d61696e7461696e6162696c6974792e737667)](https://qlty.sh/gh/renfordt/projects/colors)

A modern PHP library for effortless color manipulation and conversion. Convert between **Hex**, **RGB**, **HSL**, **HSV**, and **RAL** color formats with ease.

> 🚀 **Version 2.0** - Now powered by PHP 8.4 property hooks for a cleaner, more intuitive API!

---

✨ Features
----------

[](#-features)

- 🔄 **Universal Conversion** - Seamlessly convert between all major color formats
- 🎯 **Type-Safe** - Full type declarations with PHPStan level max compliance
- ⚡ **Modern PHP** - Leverages PHP 8.4 property hooks and latest language features
- 🎨 **RAL Support** - Complete RAL color system with closest color matching
- 🔍 **Auto-Validation** - Automatic range clamping ensures valid color values
- 📦 **Zero Dependencies** - Only requires `renfordt/clamp` utility

---

📦 Installation
--------------

[](#-installation)

```
composer require renfordt/colors
```

### Requirements

[](#requirements)

- **PHP 8.4** or higher
- **Composer**

---

🚀 Quick Start
-------------

[](#-quick-start)

### Creating Colors

[](#creating-colors)

```
use Renfordt\Colors\{HexColor, RGBColor, HSLColor, HSVColor, RALColor};

// Hex colors - with or without # prefix
$hex = HexColor::create('#FF5733');
$hex = HexColor::create('FF5733');

// RGB colors - values from 0-255
$rgb = RGBColor::create([255, 87, 51]);

// HSL colors - hue: 0-360, saturation/lightness: 0.0-1.0
$hsl = HSLColor::create([9, 1.0, 0.6]);

// HSV colors - hue: 0-360, saturation/value: 0.0-1.0
$hsv = HSVColor::create([9, 0.8, 1.0]);

// RAL colors - standard RAL codes
$ral = RALColor::create(3020);
```

---

🎨 Working with Colors (v2.0)
----------------------------

[](#-working-with-colors-v20)

### Direct Property Access

[](#direct-property-access)

The new property hooks API makes working with colors intuitive and clean:

```
// RGB - direct component access
$color = RGBColor::create([255, 100, 50]);
echo $color->red;    // 255
echo $color->green;  // 100
echo $color->blue;   // 50

// Modify individual components (automatically clamped)
$color->red = 200;
$color->green = 300;  // Automatically clamped to 255
$color->blue = -10;   // Automatically clamped to 0

// HSL - access hue, saturation, lightness
$hsl = HSLColor::create([180, 0.5, 0.7]);
echo $hsl->hue;        // 180
echo $hsl->saturation; // 0.5
echo $hsl->lightness;  // 0.7

$hsl->lightness = 0.9;  // Adjust lightness

// HSV - access hue, saturation, value
$hsv = HSVColor::create([240, 0.8, 0.6]);
echo $hsv->hue;        // 240
echo $hsv->saturation; // 0.8
echo $hsv->value;      // 0.6

// Hex - get hex value with #
$hex = HexColor::create('FF5733');
echo $hex->hex;  // #FF5733

// RAL - access RAL code
$ral = RALColor::create(3020);
echo $ral->ral;  // 3020
```

### Array Access (Still Available)

[](#array-access-still-available)

For batch operations, array methods remain available:

```
$rgb = RGBColor::create([255, 100, 50]);
[$r, $g, $b] = $rgb->getRGB();  // [255, 100, 50]

$hsl = HSLColor::create([180, 0.5, 0.7]);
[$h, $s, $l] = $hsl->getHSL();  // [180, 0.5, 0.7]

$hsv = HSVColor::create([240, 0.8, 0.6]);
[$h, $s, $v] = $hsv->getHSV();  // [240, 0.8, 0.6]
```

---

🔄 Converting Between Formats
----------------------------

[](#-converting-between-formats)

All color formats can be converted to any other format:

```
$hex = HexColor::create('#FF5733');

// Convert to any format
$rgb = $hex->toRGB();
$hsl = $hex->toHSL();
$hsv = $hex->toHSV();

// Chain conversions
$finalColor = HexColor::create('#FF5733')
    ->toRGB()
    ->toHSL()
    ->toHex();

// Precision control for HSL/HSV
$hsl = $rgb->toHSL(precision: 2);  // Round to 2 decimal places
```

### Conversion Matrix

[](#conversion-matrix)

→ Hex→ RGB→ HSL→ HSV**Hex**-✅✅✅**RGB**✅-✅✅**HSL**✅✅-✅**HSV**✅✅✅-**RAL**✅✅✅✅---

🎯 Advanced Features
-------------------

[](#-advanced-features)

### Color Utilities (HSL)

[](#color-utilities-hsl)

```
$color = HSLColor::create([200, 0.6, 0.5]);

// Brighten by 20% (default: 10%)
$color->brighten(20);

// Darken by 15%
$color->darken(15);

// Access the adjusted lightness
echo $color->lightness;  // Automatically clamped to 0.0-1.0
```

### Hex String Formatting

[](#hex-string-formatting)

```
$hex = HexColor::create('FF5733');

// Get with hash (using property)
echo $hex->hex;  // #FF5733

// Get with/without hash (using method for control)
echo $hex->getHexStr(true);   // #FF5733
echo $hex->getHexStr(false);  // FF5733

// String conversion
echo (string) $hex;  // #FF5733
```

### RAL Color Matching

[](#ral-color-matching)

Find the closest RAL color to any hex value:

```
$ral = RALColor::create(3020);
$customHex = HexColor::create('#FF5500');

// Find closest RAL color
$closest = $ral->findClosestColor($customHex);
echo $closest->ral;  // Returns the nearest RAL code
```

---

📚 API Reference
---------------

[](#-api-reference)

### RGBColor

[](#rgbcolor)

```
// Properties (with automatic validation)
$rgb->red    // int (0-255)
$rgb->green  // int (0-255)
$rgb->blue   // int (0-255)

// Methods
$rgb->getRGB()              // array
$rgb->toHex()               // HexColor
$rgb->toHSL($precision = 4) // HSLColor
$rgb->toHSV($precision = 4) // HSVColor
```

### HexColor

[](#hexcolor)

```
// Properties
$hex->hex  // string (with # prefix)

// Methods
$hex->getHexStr($withHash = true)  // string
$hex->toRGB()                       // RGBColor
$hex->toHSL($precision = 4)         // HSLColor
$hex->toHSV($precision = 4)         // HSVColor
```

### HSLColor

[](#hslcolor)

```
// Properties (with automatic validation)
$hsl->hue         // int (0-360)
$hsl->saturation  // float (0.0-1.0)
$hsl->lightness   // float (0.0-1.0)

// Methods
$hsl->getHSL()              // array{int, float, float}
$hsl->toHex()               // HexColor
$hsl->toRGB()               // RGBColor
$hsl->brighten($amount = 10) // void
$hsl->darken($amount = 10)   // void
```

### HSVColor

[](#hsvcolor)

```
// Properties (with automatic validation)
$hsv->hue         // int (0-360)
$hsv->saturation  // float (0.0-1.0)
$hsv->value       // float (0.0-1.0)

// Methods
$hsv->getHSV()  // array{int, float, float}
$hsv->toHex()   // HexColor
$hsv->toRGB()   // RGBColor
```

### RALColor

[](#ralcolor)

```
// Properties
$ral->ral  // int (RAL code)

// Methods
$ral->toHex()                         // HexColor
$ral->toRGB()                         // RGBColor
$ral->toHSL()                         // HSLColor
$ral->toHSV()                         // HSVColor
$ral->findClosestColor(HexColor $hex) // RALColor|null
```

---

🔧 Migration from v1.x to v2.0
-----------------------------

[](#-migration-from-v1x-to-v20)

Version 2.0 introduces **breaking changes** with a cleaner, modern API using property hooks.

### What Changed?

[](#what-changed)

**Removed Methods:**

- ❌ `setRGB()`, `getRed()`, `getGreen()`, `getBlue()`
- ❌ `setHexStr()`
- ❌ `setHue()`, `getHue()`, `setSaturation()`, `getSaturation()`, etc.
- ❌ `toArray()` (use `getRGB()` instead)

**New in v2.0:**

- ✅ Direct property access with automatic validation
- ✅ Property hooks ensure values stay in valid ranges
- ✅ Cleaner, more intuitive API

### Migration Examples

[](#migration-examples)

```
// ❌ Old way (v1.x)
$rgb = RGBColor::create([255, 0, 0]);
$rgb->setRGB([0, 255, 0]);
$red = $rgb->getRed();
$array = $rgb->toArray();

$hsl->setLightness(0.7);
$lightness = $hsl->getLightness();

// ✅ New way (v2.0)
$rgb = RGBColor::create([255, 0, 0]);
$rgb->red = 0;
$rgb->green = 255;
$rgb->blue = 0;
$red = $rgb->red;
$array = $rgb->getRGB();

$hsl->lightness = 0.7;
$lightness = $hsl->lightness;
```

---

🧪 Development
-------------

[](#-development)

### Running Tests

[](#running-tests)

```
# Run all tests
composer test

# Individual test suites
composer test:unit      # PHPUnit tests
composer test:types     # PHPStan static analysis
composer test:lint      # Code style check
composer test:refacto   # Rector refactoring check
```

### Code Quality

[](#code-quality)

```
# Format code
composer lint

# Apply Rector refactoring
composer refacto
```

---

🤝 Contributing
--------------

[](#-contributing)

Contributions are welcome! Please:

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

Please ensure:

- ✅ All tests pass (`composer test`)
- ✅ Code follows PSR-12 standards
- ✅ PHPStan level max compliance
- ✅ New features include tests

---

📄 License
---------

[](#-license)

This package is open-sourced software licensed under the [MIT license](LICENSE).

---

🙏 Credits
---------

[](#-credits)

Developed and maintained by [Jannik Renfordt](https://github.com/renfordt).

---

**[Documentation](https://github.com/renfordt/colors)** • **[Report Bug](https://github.com/renfordt/colors/issues)** • **[Request Feature](https://github.com/renfordt/colors/issues)**

Made with ❤️ using PHP 8.4

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance80

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~85 days

Recently: every ~52 days

Total

9

Last Release

15d ago

Major Versions

v1.0.4 → v2.0.02025-12-13

PHP version history (3 changes)v1.0.0PHP ^8.1

v1.0.3PHP ^8.2

v2.0.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/1edb79f2a1fcd61016ec9a5b65183e1f6e7852a4b1f9a5bd6f69c6160366f22c?d=identicon)[renfordt](/maintainers/renfordt)

---

Top Contributors

[![renfordt](https://avatars.githubusercontent.com/u/699163?v=4)](https://github.com/renfordt "renfordt (129 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/renfordt-colors/health.svg)

```
[![Health](https://phpackages.com/badges/renfordt-colors/health.svg)](https://phpackages.com/packages/renfordt-colors)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k430.4M1.7k](/packages/nette-utils)[dgr/nohup

A library to run a command in the background, it will return the process's pid, and get it's is running status anytime in the another process, and can be stoped anytime. It support Windows, Linux and Mac osx.

531.7k](/packages/dgr-nohup)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
