定制 awcode/awcolor 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

awcode/awcolor

Composer 安装命令:

composer require awcode/awcolor

包简介

Modern PHP color management library with conversions, manipulation, schemes and WCAG accessibility helpers across hex, rgb(a), hsl(a), hsv, cmyk and CSS named colors.

README 文档

README

A modern, dependency-free PHP color management library: convert, manipulate, build palettes and check accessibility across hex, rgb(a), hsl(a), hsv, cmyk and CSS named colors.

CI Latest Stable Version Total Downloads License

Requirements

  • PHP 8.2, 8.3 or 8.4
  • No runtime dependencies

Installation

composer require awcode/awcolor

Quick start

use AWcode\awColor;

// Construct from any common format
$color = new awColor('#ff0000');                  // hex (3, 4, 6 or 8 digits)
$color = awColor::fromString('rgb(255, 0, 0)');   // CSS rgb()/rgba()/hsl()/hsla()
$color = awColor::fromName('rebeccapurple');      // CSS3/CSS4 named color
$color = awColor::fromHsl(120, 1.0, 0.5);         // HSL factory
$color = awColor::fromHsv(0, 1.0, 1.0);           // HSV
$color = awColor::fromCmyk(0, 1, 1, 0);           // CMYK
$color = awColor::random();                       // Random opaque color

echo $color;                            // "#ff0000" (Stringable)
echo $color->getRgbaString();           // "rgba(255, 0, 0, 1)"
echo $color->getHslString();            // "hsl(0, 100%, 50%)"
print_r($color->getHsv());              // [0, 1.0, 1.0]
print_r($color->getCmyk());             // [0.0, 1.0, 1.0, 0.0]

Manipulation (immutable, chainable)

$color->lighten(0.1);          // +10% lightness
$color->darken(0.1);
$color->saturate(0.2);
$color->desaturate(0.2);
$color->rotate(60);            // hue rotation in degrees
$color->invert();
$color->grayscale();
$color->mix($other, 0.5);      // blend two colors
$color->tint(0.2);             // mix toward white
$color->shade(0.2);            // mix toward black
$color->fadeIn(0.1);
$color->fadeOut(0.1);

Palettes

$color->complement();          // single complementary awColor
$color->triadic();             // [base, +120°, +240°]
$color->tetradic();            // 4 colors at 90° spacing
$color->splitComplementary();  // [base, +150°, +210°]
$color->analogous(5, 20);      // 5 evenly spaced neighbors
$color->monochromatic(7);      // 7 luminance steps

WCAG accessibility

$fg = new awColor('#222');
$bg = new awColor('#fff');

$fg->luminance();              // WCAG 2.x relative luminance (0-1)
$fg->contrastRatio($bg);       // 21.0 for max contrast
$fg->isAccessible($bg);        // AA, normal text  (>= 4.5)
$fg->isAccessible($bg, 'AAA'); // AAA, normal text (>= 7.0)
$fg->isAccessible($bg, 'AA', 'large'); // AA, large text (>= 3.0)

// Pick the best foreground for a given background
$bg->pickReadable(new awColor('#000'), new awColor('#fff'));

Comparison & serialization

$a->equals($b);
$a->distance($b);              // Euclidean distance in RGB space
$color->toArray();             // ['hex' => ..., 'rgb' => ..., 'hsl' => ..., 'alpha' => ...]
$color->toJson();

Backwards compatibility

The original public API is preserved:

  • new awColor($hex) / new awColor($r, $g, $b) / new awColor($h, $s, $l, true)
  • Static helpers formatHex, rgbToHex, hexToRgb, rgbToHsl, hslToRgb, hexToHsl, hslToHex
  • Instance helpers getHex/getRgb/getR/G/B/getHsl/getH/S/L, setHex/setRgb/setHsl/setR/G/B, isLight/isDark/isGrey, complementary()

Two long-standing bugs were fixed in the process:

  • hslToRgb() now returns 0-255 integers instead of 0-1 floats (which caused rgbToHex() to always return black for HSL-derived values).
  • setHsl() previously wrote to a typo'd _rbg property — it now correctly updates _rgb and _hex.

Testing

composer install
composer test

awcode/awcolor 适用场景与选型建议

awcode/awcolor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「color」 「rgb」 「hsl」 「hex」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 awcode/awcolor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 awcode/awcolor 我们能提供哪些服务?
定制开发 / 二次开发

基于 awcode/awcolor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 53
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-14