Iris - PHP Color Library

PHP library for color manipulation and conversion.

For PHP 7.3 or PHP 7.4, use version ^2.5, and check the php-7 branch.

Install

Via Composer

$ composer require ozdemirburak/iris

Usage

Hex

use OzdemirBurakIrisColorHex; $hex = new Hex('#ff00ff'); // same as new Hex('fuchsia'); echo $hex->red(); // ff echo $hex->green(); // 00 echo $hex->blue(); // ff echo $hex->values(); // ['ff', '00', 'ff'] $hexa = $hex->toHexa(); // OzdemirBurakIrisColorHexa('ff00ffff') $hsl = $hex->toHsl(); // OzdemirBurakIrisColorHsl('300,100,50') $hsla = $hex->toHsla(); // OzdemirBurakIrisColorHsla('300,100,50,1.0') $hsv = $hex->toHsv(); // OzdemirBurakIrisColorHsv('300,100,100') $rgb = $hex->toRgb(); // OzdemirBurakIrisColorRgb('255,0,255') $rgba = $hex->toRgba(); // OzdemirBurakIrisColorRgba('255,0,255,1.0') echo $hex; // #ff00ff

Hexa (hex with alpha value)

use OzdemirBurakIrisColorHexa; $hexa = new Hexa('#ff00ff4c'); echo $hexa->red(); // ff echo $hexa->green(); // 00 echo $hexa->blue(); // ff echo $hexa->alpha(); // 0.3 - as a float for compatibility with the other conversions echo $hexa->values(); // ['ff', '00', 'ff', 0.3] $hsl = $hexa->toHsl(); // OzdemirBurakIrisColorHsl('300,100,50') $hsla = $hexa->toHsla(); // OzdemirBurakIrisColorHsla('300,100,50,0.3') $hsv = $hexa->toHsv(); // OzdemirBurakIrisColorHsv('300,100,100') $rgb = $hexa->toRgb(); // OzdemirBurakIrisColorRgb('255,0,255') $rgba = $hexa->toRgba(); // OzdemirBurakIrisColorRgba('255,0,255,0.3') echo $hexa; // #ff00ff4c

HSL

use OzdemirBurakIrisColorHsl; $hsl = new Hsl('hsl(300,100%,50%)'); // same as new Hsl('fuchsia'); echo $hsl->hue(); // 300 echo $hsl->saturation(); // 100 echo $hsl->lightness(); // 50 $values = $hsl->values(); // [300, '100%', '50%'] $normalizedValues = $hsl->valuesInUnitInterval(); // [300/360, 100/100, 50/100] $hex = $hsl->toHex(); // OzdemirBurakIrisColorHex('ff00ff') $hexa = $hsl->toHexa(); // OzdemirBurakIrisColorHexa('ff00ffff') $hsv = $hsl->toHsv(); // OzdemirBurakIrisColorHsv('300,100,100') $rgb = $hsl->toRgb(); // OzdemirBurakIrisColorRgb('255,0,255') $rgba = $hsl->toRgba(); // OzdemirBurakIrisColorRgba('255,0,255,1.0') echo $hsl; // hsl(300,100%,50%)

HSLA

use OzdemirBurakIrisColorHsla; $hsla = new Hsla('hsla(150,100%,50%,0.3)'); echo $hsla->hue(); // 150 echo $hsla->saturation(); // 100 echo $hsla->lightness(); // 50 echo $hsla->alpha(); // 0.3 $values = $hsla->values(); // [150, '100%', '50%', 0.3] $hex = $hsla->toHex(); // OzdemirBurakIrisColorHex('b2ffd8') $hex = $hsla->toRgba(); // OzdemirBurakIrisColorRgba('0,255,128,0.3') $hexa = $hsla->toHexa(); // OzdemirBurakIrisColorHexa('ff00ff4c') echo $hsla; // hsla(150,100%,50%,0.3)

HSV

use OzdemirBurakIrisColorHsv; $hsv = new Hsv('hsv(300,100%,100%)'); // same as new Hsv('fuchsia'); echo $hsv->hue(); // 300 echo $hsv->saturation(); // 100 echo $hsv->value(); // 100 $values = $hsv->values(); // [100, '100%', '100%'] $normalizedValues = $hsv->valuesInUnitInterval(); // [300/360, 100/100, 100/100] $hex = $hsv->toHex(); // OzdemirBurakIrisColorHex('ff00ff') $hexa = $hsv->toHexa(); // OzdemirBurakIrisColorHexa('ff00ffff') $hsl = $hsv->toHsl(); // OzdemirBurakIrisColorHsl('300,100,50') $hsla = $hsv->toHsla(); // OzdemirBurakIrisColorHsla('300,100,50,1.0') $hsv = $hsv->toHsv(); // OzdemirBurakIrisColorHsv('300,100,100') $rgb = $hsv->toRgb(); // OzdemirBurakIrisColorRgb('255,0,255') $rgba = $hsv->toRgba(); // OzdemirBurakIrisColorRgba('255,0,255,1.0') echo $hsv; // hsl(300,100%,100%)

RGB

use OzdemirBurakIrisColorRgb; $rgb = new Rgb('rgb(255, 0, 255)'); // same as new Rgb('fuchsia'); echo $rgb->red(); // 255 echo $rgb->green(); // 0 echo $rgb->blue(); // 255 $values = $rgb->values(); // [255, 0, 255] $hex = $rgb->toHex(); // OzdemirBurakIrisColorHex('ff00ff') $hexa = $rgb->toHexa(); // OzdemirBurakIrisColorHexa('ff00ffff') $hsl = $rgb->toHsl(); // OzdemirBurakIrisColorHsl('300,100,50') $hsla = $rgb->toHsla(); // OzdemirBurakIrisColorHsla('300,100,50,1.0') $hsv = $rgb->toHsv(); // OzdemirBurakIrisColorHsv('300,100,100') $rgb = $rgb->toRgb(); // OzdemirBurakIrisColorRgb('255,0,255') $rgba = $rgb->toRgba(); // OzdemirBurakIrisColorRgba('255,0,255,1.0') echo $rgb; // rgb(255,0,255)

RGBA

use OzdemirBurakIrisColorRgba; $rgba = new Rgba('rgba(93,111,222,0.33)'); echo $rgba->red(); // 93 echo $rgba->green(); // 111 echo $rgba->blue(); // 222 echo $rgba->alpha(); // 0.33, $hex = $rgba->background((new Hex('ccc'))->toRgb())->toHex(); // OzdemirBurakIrisColorHex('a7add1') $hexa = $rgba->toHexa(); // OzdemirBurakIrisColorHexa('a7add154') echo $rgba; // rgba(127,127,127,0.5)

CMYK

use OzdemirBurakIrisColorCmyk; $cmyk = new Cmyk('cmyk(0,100,0,0'); echo $cmyk->cyan(); // 0 echo $cmyk->magenta(); // 100 echo $cmyk->yellow(); // 0 echo $cmyk->black(); // 0 $values = $cmyk->values(); // [0, 100, 0, 0] $hex = $cmyk->toHex(); // OzdemirBurakIrisColorHex('ff00ff') $hexa = $cmyk->toHexa(); // OzdemirBurakIrisColorHexa('ff00ffff') $hsl = $cmyk->toHsl(); // OzdemirBurakIrisColorHsl('300,100,50') $hsla = $cmyk->toHsla(); // OzdemirBurakIrisColorHsla('300,100,50,1.0') $hsv = $cmyk->toHsv(); // OzdemirBurakIrisColorHsv('300,100,100') $rgb = $cmyk->toRgb(); // OzdemirBurakIrisColorRgb('255,0,255') $rgba = $cmyk->toRgba(); // OzdemirBurakIrisColorRgba('255,0,255,1.0') echo $cmyk; // cmyk(0,100,0,0)

Via Factory

If you do not know what the color string will be (for example, you're getting it from a group of rows from a database), then you can try using Factory to instantiate an appropriate color class:

use OzdemirBurakIrisColorFactory; $color = Factory::init('rgba(93,111,222,0.33)'); echo $color->red(); // 93 echo $color->green(); // 111 echo $color->blue(); // 222 echo $color->alpha(); // 0.33

Color Cloning

You can clone a color object to make a copy and modify it as needed.

use OzdemirBurakIrisColorHex; $original = new Hex('#b2b2b2'); $cloned = $original->clone()->toHexa()->alpha(0.5); // OzdemirBurakIrisColorHexa('#b2b2b27f')

Color Manipulation

Saturation

Saturate or desaturate by a percent.

echo (new Hsl('90,80%,50%'))->saturate(20)->toHex(); // #80ff00 echo (new Hsl('90, 80%, 50%'))->desaturate(20)->toRgb(); // rgb(128,204,51) echo (new Hex('#80cc33'))->grayscale(); // #808080, same as desaturate 100

Lightness

Lighten, darken or brighten by a percent.

$hex = new Hex('#333'); echo $hex->lighten(20); // #666666 echo $hex->darken(20); // #000000 echo $hex->brighten(20); // #666666

Spin

Spin by an angle [-360, 360]

$hex = (new Hsl('10,90%,50'))->spin(30)->toHex(); echo $hex; // #f2a60d

Mix

Mix by a percent.

$hex = new Hex('#000'); echo $hex->mix(new Hex('#fff'), 50); // #808080

Tint

Mix color with white by a percent.

$hex = new Hex('#000'); echo $hex->tint(50); // #808080

Shade

Mix color with black by a percent.

$hex = new Hex('#FFF'); echo $hex->shade(50); // #808080

Fade

Set the absolute opacity of a color by a percent.

$hsl = new Hsl('90,90,50'); echo $hsl->fade(10); // hsla(90,90%,50%,0.1) $rgb = new Rgb('128,242,13'); echo $rgb->fade(10); // rgba(128,242,13,0.1)

FadeIn

Increase the opacity of a color by a percent.

$hsla = new Hsla('90,90,50,0.3'); echo $hsla->fadeIn(10); // hsla(90,90%,50%,0.4) $rgba = new Rgba('128,242,13,0.3'); echo $rgba->fadeIn(10); // rgba(128,242,13,0.4)

FadeOut

Decrease the opacity of a color by a percent.

$hsla = new Hsla('90,90,50,0.3'); echo $hsla->fadeOut(10); // hsla(90,90%,50%,0.2) $rgba = new Rgba('128,242,13,0.3'); echo $rgba->fadeOut(10); // rgba(128,242,13,0.2)

Is light or dark

Determine if color is dark or light color.

$hex = new Hex('#000'); echo $hex->isLight(); // false echo $hex->isDark(); // true

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Credits

Burak Özdemir All Contributors

License

The MIT License (MIT). Please see License File for more information.

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。