ozdemirburak/iris
Composer 安装命令:
composer require ozdemirburak/iris
包简介
PHP library for color manipulation and conversion.
关键字:
README 文档
README
PHP library for color manipulation and conversion. Requires PHP 8.1+ and supports Hex, Hexa, RGB, RGBA, HSL, HSLA, HSV, CMYK, and OKLCH color formats.
Install
$ composer require ozdemirburak/iris
Usage
Hex
use OzdemirBurak\Iris\Color\Hex; $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(); // \OzdemirBurak\Iris\Color\Hexa('ff00ffff') $hsl = $hex->toHsl(); // \OzdemirBurak\Iris\Color\Hsl('300,100,50') $hsla = $hex->toHsla(); // \OzdemirBurak\Iris\Color\Hsla('300,100,50,1.0') $hsv = $hex->toHsv(); // \OzdemirBurak\Iris\Color\Hsv('300,100,100') $rgb = $hex->toRgb(); // \OzdemirBurak\Iris\Color\Rgb('255,0,255') $rgba = $hex->toRgba(); // \OzdemirBurak\Iris\Color\Rgba('255,0,255,1.0') $oklch = $hex->toOklch(); // \OzdemirBurak\Iris\Color\Oklch('70.17,0.3225,328.36') echo $hex; // #ff00ff
Hexa (hex with alpha value)
use OzdemirBurak\Iris\Color\Hexa; $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(); // \OzdemirBurak\Iris\Color\Hsl('300,100,50') $hsla = $hexa->toHsla(); // \OzdemirBurak\Iris\Color\Hsla('300,100,50,0.3') $hsv = $hexa->toHsv(); // \OzdemirBurak\Iris\Color\Hsv('300,100,100') $rgb = $hexa->toRgb(); // \OzdemirBurak\Iris\Color\Rgb('255,0,255') $rgba = $hexa->toRgba(); // \OzdemirBurak\Iris\Color\Rgba('255,0,255,0.3') echo $hexa; // #ff00ff4c
HSL
use OzdemirBurak\Iris\Color\Hsl; $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(); // \OzdemirBurak\Iris\Color\Hex('ff00ff') $hexa = $hsl->toHexa(); // \OzdemirBurak\Iris\Color\Hexa('ff00ffff') $hsv = $hsl->toHsv(); // \OzdemirBurak\Iris\Color\Hsv('300,100,100') $rgb = $hsl->toRgb(); // \OzdemirBurak\Iris\Color\Rgb('255,0,255') $rgba = $hsl->toRgba(); // \OzdemirBurak\Iris\Color\Rgba('255,0,255,1.0') echo $hsl; // hsl(300,100%,50%)
HSLA
use OzdemirBurak\Iris\Color\Hsla; $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(); // \OzdemirBurak\Iris\Color\Hex('b2ffd8') $rgba = $hsla->toRgba(); // \OzdemirBurak\Iris\Color\Rgba('0,255,128,0.3') $hexa = $hsla->toHexa(); // \OzdemirBurak\Iris\Color\Hexa('00ff804d') echo $hsla; // hsla(150,100%,50%,0.3)
HSV
use OzdemirBurak\Iris\Color\Hsv; $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(); // [300, '100%', '100%'] $normalizedValues = $hsv->valuesInUnitInterval(); // [300/360, 100/100, 100/100] $hex = $hsv->toHex(); // \OzdemirBurak\Iris\Color\Hex('ff00ff') $hexa = $hsv->toHexa(); // \OzdemirBurak\Iris\Color\Hexa('ff00ffff') $hsl = $hsv->toHsl(); // \OzdemirBurak\Iris\Color\Hsl('300,100,50') $hsla = $hsv->toHsla(); // \OzdemirBurak\Iris\Color\Hsla('300,100,50,1.0') $hsv = $hsv->toHsv(); // \OzdemirBurak\Iris\Color\Hsv('300,100,100') $rgb = $hsv->toRgb(); // \OzdemirBurak\Iris\Color\Rgb('255,0,255') $rgba = $hsv->toRgba(); // \OzdemirBurak\Iris\Color\Rgba('255,0,255,1.0') echo $hsv; // hsv(300,100%,100%)
RGB
use OzdemirBurak\Iris\Color\Rgb; $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(); // \OzdemirBurak\Iris\Color\Hex('ff00ff') $hexa = $rgb->toHexa(); // \OzdemirBurak\Iris\Color\Hexa('ff00ffff') $hsl = $rgb->toHsl(); // \OzdemirBurak\Iris\Color\Hsl('300,100,50') $hsla = $rgb->toHsla(); // \OzdemirBurak\Iris\Color\Hsla('300,100,50,1.0') $hsv = $rgb->toHsv(); // \OzdemirBurak\Iris\Color\Hsv('300,100,100') $rgb = $rgb->toRgb(); // \OzdemirBurak\Iris\Color\Rgb('255,0,255') $rgba = $rgb->toRgba(); // \OzdemirBurak\Iris\Color\Rgba('255,0,255,1.0') echo $rgb; // rgb(255,0,255)
RGBA
use OzdemirBurak\Iris\Color\Rgba; $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(); // \OzdemirBurak\Iris\Color\Hex('a7add1') $hexa = $rgba->toHexa(); // \OzdemirBurak\Iris\Color\Hexa('5d6fde54') - preserves original RGB echo $rgba; // rgba(93,111,222,0.33)
CMYK
use OzdemirBurak\Iris\Color\Cmyk; $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(); // OzdemirBurak\Iris\Color\Hex('ff00ff') $hexa = $cmyk->toHexa(); // OzdemirBurak\Iris\Color\Hexa('ff00ffff') $hsl = $cmyk->toHsl(); // OzdemirBurak\Iris\Color\Hsl('300,100,50') $hsla = $cmyk->toHsla(); // OzdemirBurak\Iris\Color\Hsla('300,100,50,1.0') $hsv = $cmyk->toHsv(); // OzdemirBurak\Iris\Color\Hsv('300,100,100') $rgb = $cmyk->toRgb(); // OzdemirBurak\Iris\Color\Rgb('255,0,255') $rgba = $cmyk->toRgba(); // OzdemirBurak\Iris\Color\Rgba('255,0,255,1.0') echo $cmyk; // cmyk(0,100,0,0)
OKLCH
use OzdemirBurak\Iris\Color\Oklch; use OzdemirBurak\Iris\Color\Hex; // Comma-separated syntax $oklch = new Oklch('oklch(70%, 0.15, 150)'); // Space-separated syntax (CSS Color Level 4) $oklch = new Oklch('oklch(40.1% 0.123 21.57)'); $oklch = new Oklch('oklch(59.69% 0.156 49.77)'); echo $oklch->lightness(); // 59.69 echo $oklch->chroma(); // 0.156 echo $oklch->hue(); // 49.77 $values = $oklch->values(); // [59.69, 0.156, 49.77] $hex = $oklch->toHex(); // OzdemirBurak\Iris\Color\Hex $rgb = $oklch->toRgb(); // OzdemirBurak\Iris\Color\Rgb $hsl = $oklch->toHsl(); // OzdemirBurak\Iris\Color\Hsl echo $oklch; // oklch(59.69% 0.156 49.77) // Convert from other formats to OKLCH $hex = new Hex('#ff0000'); $oklch = $hex->toOklch(); echo $oklch; // oklch(62.8% 0.2577 29.23)
Note: The following are not supported:
- Alpha values (e.g.,
oklch(59.69% 0.156 49.77 / .5)) - alpha is ignored - Relative color syntax with
fromkeyword (e.g.,oklch(from green l c h))
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 OzdemirBurak\Iris\Color\Factory; $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 OzdemirBurak\Iris\Color\Hex; $original = new Hex('#b2b2b2'); $cloned = $original->clone()->toHexa()->alpha(0.5); // OzdemirBurak\Iris\Color\Hexa('#b2b2b280')
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
Gradient
Generate an array of colors (gradient) between two or more colors.
use OzdemirBurak\Iris\Color\Hex; use OzdemirBurak\Iris\Color\Rgb; // Two-color gradient: 5 steps from red to blue $gradient = (new Hex('#ff0000'))->gradient(new Hex('#0000ff'), 5); // ['#ff0000', '#bf0040', '#800080', '#4000bf', '#0000ff'] // Multi-color gradient with pivot: red → yellow → green in 7 steps $gradient = (new Hex('#ff0000'))->gradient([new Hex('#ffff00'), new Hex('#00ff00')], 7); // ['#ff0000', '#ff5500', '#ffaa00', '#ffff00', '#aaff00', '#55ff00', '#00ff00'] // Works with any color type - output matches the starting color type $gradient = (new Rgb('255,0,0'))->gradient(new Rgb('0,0,255'), 3); // [rgb(255,0,0), rgb(128,0,128), rgb(0,0,255)]
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
ozdemirburak/iris 适用场景与选型建议
ozdemirburak/iris 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.84M 次下载、GitHub Stars 达 120, 最近一次更新时间为 2017 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「conversion」 「color」 「manipulation」 「transformation」 「rgb」 「cmyk」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ozdemirburak/iris 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ozdemirburak/iris 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ozdemirburak/iris 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
On-demand image manipulation service.
Laravel package for resized.co, the on-demand image manipulation service.
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Manipulate images in pure PHP
Native (browser) color input field for SilverStripe
Colorizes the Flarum forum header and interface based on the active tag's color.
统计信息
- 总下载量: 1.84M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 122
- 点击次数: 30
- 依赖项目数: 19
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-30