byteforge/color-randomizer

这可以在各种颜色情况下使用。例如生成随机颜色等更多精彩功能。

v1.0.3 2024-04-09 17:09 UTC

This package is auto-updated.

Last update: 2024-09-16 05:03:07 UTC


README

概述

Color Randomizer 是一款综合性的颜色操作和生成工具包。它旨在提供从生成随机颜色到颜色格式转换等多种功能,满足开发者和设计师的各种需求。拥有超过450种颜色的库,Color Randomizer 确保了输出的准确性和多样性。主要功能包括:

  • 生成单个或多个随机颜色
  • 在十六进制和RGB颜色格式之间转换
  • 访问超过450种特色颜色库
  • 比较两种颜色以确定它们之间的差异
  • 计算颜色亮度
  • 从RGB字符串中提取RGB组件
  • 检索450多种颜色的HTML颜色代码
  • 为给定的颜色代码找到最接近匹配的颜色名称
  • 发现相似或互补的颜色
  • 以美观的方式展示所有支持的颜色

安装

您可以通过 Composer 安装此包,

composer require byteforge/color-randomizer

您只需要 laravel^6 就可以轻松安装 Color Randomizer 包。无需额外的依赖项;只需坚持使用默认的 Laravel 设置。

使用指南

生成单个随机颜色

Color Randomizer 中的 getSingleColor 函数提供了一种灵活且可定制的生成随机颜色的方法,适用于各种应用场景,如动态更改网站背景。以下是如何在不同场景下使用此函数的示例。

示例用法

use ByteForge\ColorRandomizer\ColorRandomizer;
 
// Example code...

// Generating a color without its name
$color = ColorRandomizer::getSingleColor(hex: true, minBrightness: 30, maxBrightness: 255, withMostMatchedName: false);

// Generating a color with its name
$color = ColorRandomizer::getSingleColor(hex: true, minBrightness: 30, maxBrightness: 255, withMostMatchedName: true, formattedName: true);

参数说明

  • hex:确定返回颜色代码的格式。true 为十六进制格式,false 为 RGB 格式。
  • minBrightness & maxBrightness:控制生成颜色的亮度,值越大颜色越亮,值越小颜色越暗。注意:$maxBrightness 应不超过 255。
  • withMostMatchedName:当设置为 true 时,函数返回包含名称和颜色代码的数组,提供最接近匹配的颜色名称。当为 false 时,仅返回颜色代码作为字符串。
  • formattedName:当 $withMostMatchedName 为 true 时,指定返回颜色名称的格式。true 为格式化名称(例如,“Alice Blue”),false 为原始名称(例如,“alice blue”)。利用这些功能,用户可以轻松地将动态颜色功能集成到项目中,提升美感和用户体验。

生成多个随机颜色

getMultipleColors 这个函数利用了 getSingleColor 的所有功能,它只是额外添加了一个参数,即您想要获取的颜色数量。它将返回包含所有颜色的数组,或者包含名称和颜色代码的所有数组的数组。

示例用法

// Other Codes..
$colors = ColorRandomizer::getMultipleColors(colorsAmount: 4, hex: true, minBrightness: 30, maxBrightness: 255, withMostMatchedName: true, formattedName: true); // returns an array like, [['name'=> Blue, 'color_code' => '#0000FF'], ['name' => 'Alice Blue', 'color_code' => '#F0F8FF' ...]]

$colors = ColorRandomizer::getMultipleColors(colorsAmount: 4, hex: true, minBrightness: 30, maxBrightness: 255, withMostMatchedName: false) ;// returns an array like, ['#ED872D', '#483D8B', '#DE3163' ...]

访问包含450+种颜色数组的工具

非常简单,您可以通过 ColorRandomizer::getColors() 或 ColorRandomizer::$colors 在一秒钟内轻松获取我的450+种颜色努力工作。

示例用法

// 1st way
$colors = ColorRandomizer::getColors(formatNames: true); // You are able to Format Names through The function, This makes it better than the 2nd way.

// 2nd way
$colors = ColorRandomizer::$colors; // Color Names wont be formatted

RGB TO HEX & HEX TO RGB

我们提供将 RGB 转换为十六进制和将十六进制转换为 RGB 的最佳且最简单的方法。

示例用法

// RGB To Hex

// 1st Way
$hex = ColorRandomizer::rgbToHex(fullRGB: "5, 91, 230"); // Make Sure You Specify The Argument Name Like 'fullRGB: $myRGB'. You can also give the RGB like rgb(5, 91, 230),
// Returns #055be6

// 2nd Way
$hex = ColorRandomizer::rgbToHex(r: 5, g: 91, b: 230); // Make Sure You Specify The Argument Name Like 'r:, g: and b:',
// Returns #055be6

// Hex To RGB
$rgb = ColorRandomizer::hexToRgb(hex: '#055be6');
// returns rgb(5, 91, 230)

计算亮度和两种颜色之间的差异

我们提供90%以上的准确结果。简单且是颜色计算的最佳方式。

示例用法

// Get Color Difference Between 2 colors
$colorDifference = ColorRandomizer::colorDifference(color1: '#0548e6', color2: '#05a6e6', hex: true); // Hex refers that your color1 and color2 is Hex or RGB.
// returns 94. Similar Colors Threshold Could Be 130

// Get Color Brightness
$colorBrightness = ColorRandomizer::calculateBrightness(rgbOrHex: '#0548e6', type: 'hex'); // type must be hex or rgb, Its for verifying your Color code Type.
// returns 69

从全RGB中提取红、绿、蓝

只需一行代码即可从全RGB中提取红、绿、蓝。

示例用法

$rgbArray = ColorRandomizer::extractRGBComponents(colorString: "rgb(5, 91, 230)") // returns ['r' => 5, 'g' => 91, 'b' => 230'];

从颜色名称获取颜色代码

我们支持超过450种颜色,并可以通过颜色名称搜索以获取最准确的颜色代码。

$colorCode = ColorRandomizer::getCodesFromHtmlColors(hex: true, name: 'dandelion') // returns #F0E130;

从颜色代码获取最匹配的颜色名称

最佳颜色名称查找功能,颜色代码匹配准确率超过95%。

$colorName = ColorRandomizer::getNearestNameFromCode(hex: true, rgbOrHex: '#05e676'); // returns 'spring green'
// You can use ucwords($colorName) to format the words 1st letter to uppercase

获取相似颜色

相似颜色功能始终帮助您获取与您提供的颜色相似的颜色/相关颜色(声明:对于自定义颜色,为了满足颜色数量,您可以使用自定义颜色,但它可能会使您的网页速度变慢。因此,不使用自定义颜色是首选的)

$similarColors = ColorRandomizer::getSimilarColors(rgbOrHex: '#FFB7C5', goalAmount: 7, hex: true, maxDifference: 100, minDifference: 0.5, allowCustomColors: false);
/* It'll return an array containing other arrays like,
[["name" => "Carnation Pink", "hex" => "FFA6C9", "rgb" => "rgb(255, 166, 201)", "difference" => 17.464249196573] ... more]
*/ 

所有颜色页面配置

配置所有颜色页面是否可见以及页面的路径。

在ENV上配置

COLOR_RANDOMIZER_COLOR_PAGE_ROUTE = '/all-colors'
COLOR_RANDOMIZER_COLOR_PAGE = true