syholloway/mrcolor

PHP 的颜色操作工具和格式转换

0.0.1 2013-10-10 11:17 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:00:47 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

MrColor

MrColor 是一个针对 PHP 的颜色操作库。

查看 docs/example.php 文件以及此处的 wiki(https://github.com/syholloway/mrcolor/wiki),以获取一些有用的知识。

此包需要 PHP 5.3,且遵循 PSR-0 和 PSR-1 规范,我尽量遵循 PSR-2,但不能保证完全符合。

基本功能

<?php

use SyHolloway\MrColor\Color;

// Create a new color object with the default value of black
$color = Color::create();

// Create a new color object setting the red, green and blue values
$color2 = Color::create(array(
    'red' => 200,
    'green' => 200,
    'blue' => 100
));

现在您有了 MrColor 对象,您可以像属性一样读取和写入颜色格式。

<?php

use SyHolloway\MrColor\Color;

// Create a new color object setting the hue, saturation and lightness values
$color = Color::create(array(
    'hue' => 200,
    'saturation' => 0.5,
    'lightness' => 0.9
));

// The object was built with hsl, but format conversion happens on the fly
echo 'Hue = ' . $color->hue;
echo 'Red = ' . $color->red;
echo 'Hex = #' . $color->hex;

// Feel free to edit these properties...
$color->blue = 123; // Set blue to 123 on the 0-255 scale
$color->green -= 20; // 20 less green on the 0-255 scale
$color->lightness += 0.23; // 23% lighter

// ...And expect the update to happen across all formats
echo $color->hue;

现在您可以使用扩展(附加到对象的方法)来执行一些强大的操作。

<?php

use SyHolloway\MrColor\Color;

// Create a new color object setting the red value
$red = Color::create(array(
    'red' => 200
));

// Create another color object setting the blue value
$blue = Color::create(array(
    'blue' => 200
));

// Lighten the red by 8%
$red->lighten(8);

// Darken the blue by 12%
$blue->darken(12);

// Using the merge function on its own would alter the object it was used on.
	// So first we will clone one of the colors
	$copyOfBlue = $blue->copy();

	// We can now merge the copy of blue with red to make purple
	$copyOfBlue->merge($red);
	$purple = $copyOfBlue;

// This could all be alot tidier if we do some method chaining,
// because merge() both alters the object and returns it we can do this:
$purple = $blue->copy()->merge($red);

// Now if we want to get the complementry color to purple we can do so 
// using the getComplementary() method.
$yellowyGreen = $purple->getComplementary();

请记住,如果您使用的是修改并返回颜色对象的扩展,并且希望使用的对象保持不变,请像上面的示例中那样使用 copy() 方法首先创建对象的克隆。如果我在合并之前没有复制 $blue,那么 ($blue === $purple)。

高级功能

查看 Wiki 了解如何在不编辑源文件的情况下扩展 MrColor。

与 composer 一起使用

如果您想通过 composer 使用此包,可以将它添加到您的应用的 composer.json 文件中,如下所示:

"require" {
	"syholloway/mrcolor" : "0.*"
}

现在,在您的应用的 composer.json 文件位置运行 composer update 命令。

不使用 composer 使用

如果您想不使用 composer 使用此包,可以使用以下 2 行代码启动一个为此包配置好的自动加载器:

<?php

use SyHolloway\MrColor\Color;

require_once('path/to/this/package/' . 'manual-init.php');