popphp/pop-color

Pop Color Component for Pop PHP Framework

1.0.0 2023-11-09 04:26 UTC

This package is auto-updated.

Last update: 2024-11-19 03:11:13 UTC


README

Build Status Coverage Status

Join the chat at https://popphp.slack.com Join the chat at https://discord.gg/TZjgT74U7E

Overview

Pop Color is a helpful component to manage different types of color values and conversions. Supported color formats include:

  • RGB
  • HEX
  • HSL
  • CMYK
  • Grayscale

Within the Pop PHP Framework, the pop-css, pop-image and pop-pdf components use this component.

Top

Install

Install pop-color using Composer.

composer require popphp/pop-color

Or, require it in your composer.json file

"require": {
    "popphp/pop-color" : "^1.0.0"
}

Top

Quickstart

Create a color object

$rgb = Color::rgb(120, 60, 30, 0.5);
echo $rgb . PHP_EOL;

The above command will print the default CSS format:

rgba(120, 60, 30, 0.5)

Convert to another color format

$hex = $rgb->toHex();
echo $hex . PHP_EOL;
#783c1e
$hsl = $hex->toHsl();
echo $hsl . PHP_EOL;
hsl(20, 75%, 47%)
// Will print a string of space-separated values, common to the PDF color string format
$cmyk = $rgb->toCmyk();
echo $cmyk . PHP_EOL; 
0 0.5 0.75 0.53

Accessing Color Properties

$rgb = Color::rgb(120, 60, 30, 0.5);
echo $rgb->getR() . PHP_EOL;
echo $rgb->getG() . PHP_EOL;
echo $rgb->getB() . PHP_EOL;
echo $rgb->getA() . PHP_EOL;
120
60
30
0.5
$cmyk = Color::cmyk(60, 30, 20, 50);
echo $cmyk->getC() . PHP_EOL;
echo $cmyk->getM() . PHP_EOL;
echo $cmyk->getY() . PHP_EOL;
echo $cmyk->getK() . PHP_EOL;
60
30
20
50

Parse Color Strings

$rgb = Color::parse('rgba(120, 60, 30, 0.5)');
echo $rgb->getR() . PHP_EOL;
echo $rgb->getG() . PHP_EOL;
echo $rgb->getB() . PHP_EOL;
echo $rgb->getA() . PHP_EOL;
echo $rgb . PHP_EOL;
120
60
30
0.5
rgba(120, 60, 30, 0.5)

Top