定制 breadthe/php-contrast 二次开发

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

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

breadthe/php-contrast

Composer 安装命令:

composer require breadthe/php-contrast

包简介

Color and contrast tools

README 文档

README

Latest Version on Packagist Build Status Quality Score Total Downloads

Provides various utilities for working with color contrast.

The primary purpose is to facilitate easy generation of accessible (minimum 3.0 contrast ratio) hex color pairs. A variation of these tools restricts the generated colors to TailwindCSS's default palette.

I've created a demo page to show how color pairs can be generated by this package.

Installation

You can install the package via composer:

composer require breadthe/php-contrast

Usage

Import the class.

use Breadthe\PhpContrast\HexColor; // factory

use Breadthe\PhpContrast\HexColorPair; // hex pair utilities

use Breadthe\PhpContrast\TailwindColor; // Tailwind color pair utilities

Check the contrast ratio between 2 colors

$hexColorPair = HexColorPair::make(HexColor::make('000000'), HexColor::make('ffffff'));
$hexColorPair->ratio; // 21
$hexColorPair->fg; // '#000000'
$hexColorPair->bg; // '#ffffff'

Get a random color pair (with the resulting ratio), with minimum 3:1 contrast ratio

$hexColorPair = HexColorPair::random();
$hexColorPair->ratio; // 3.8
$hexColorPair->fg->hex; // '#36097e'
$hexColorPair->fg->name; // null
$hexColorPair->bg->hex; // '#ed4847'
$hexColorPair->bg->name; // null

Get a random color pair (with the resulting ratio), with minimum specified contrast ratio (but no less than 3:1)

⚠️ Warning For performance reasons, the minimum requested contrast ratio is capped at 4.5, although the generated pairs can go up to the theoretical maximum 21:1 ratio.

⚠️ Caution When chaining with minContrast(), make sure to use getRandom() instead of random().

$hexColorPair = HexColorPair::minContrast(4.5)->getRandom();
$hexColorPair->ratio; // 7.6
$hexColorPair->fg->hex; // '#0c402f'
$hexColorPair->fg->name; // null
$hexColorPair->bg->hex; // '#badd73'
$hexColorPair->bg->name; // null

Get a random accessible sibling for the given color, with minimum specified contrast ratio (but no less than 3:1)

// Minimum 3:1 contrast ratio
HexColorPair::sibling('000000')->hex; // '#ffffff'

// Minimum specified contrast ratio (no less than 3:1)
HexColorPair::minContrast(4.5)->getSibling('000000')->hex; // '#ffffff'

Generate a random TailwindCSS color

$twColor = TailwindColor::random();
$twColor->hex; // '#e2e8f0'
$twColor->name; // 'gray-300'

Generate a pair of random accessible TailwindCSS colors

$twColorpair = TailwindColor::randomPair();
$twColorpair->ratio; // 3.3
$twColorpair->fg->hex; // '#63b3ed'
$twColorpair->fg->name; // 'blue-400'
$twColorpair->bg->hex; // '#4a5568'
$twColorpair->bg->name; // 'green-700'

Generate a pair of random accessible TailwindCSS colors, with minimum specified contrast ratio (but no less than 3:1)

⚠️ Warning For performance reasons, the minimum requested contrast ratio is capped at 4.5, although the generated pairs can go up to the theoretical maximum 21:1 ratio.

⚠️ Caution When chaining with minContrast(), make sure to use getRandomPair() instead of randomPair().

$twColorpair = TailwindColor::minContrast(4.5)->getRandomPair();
$twColorpair->ratio; // 7.0
$twColorpair->fg->hex; // '#faf5ff'
$twColorpair->fg->name; // 'purple-100'
$twColorpair->bg->hex; // '#9b2c2c'
$twColorpair->bg->name; // 'red-800'

Merge the default Tailwind colors with a custom palette

You may extend the default Tailwind colors with your own custom palette. Here's an example of how to import a custom palette from a JSON file.

{
    "background": "#FFFFFF",
    "headline": "#1f1235",
    "sub-headline": "#1b1425",
    "button": "#ff6e6c",
    "button-text": "#1f1235"
}
$customPalette = json_decode(file_get_contents('custom-palette.json'), true);

$colors = TailwindColor::merge($customPalette)->getColors();

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email omigoshdev@protonmail.com instead of using the issue tracker.

Credits

License

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

PHP Package Boilerplate

This package was generated using the PHP Package Boilerplate.

breadthe/php-contrast 适用场景与选型建议

breadthe/php-contrast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.31k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 02 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 breadthe/php-contrast 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-20