dobby007/mrcolor

PHP的颜色操作工具和格式转换。这是 https://github.com/syholloway/mrcolor 的分支。原始库的包名是 'syholloway/mrcolor'

0.0.1 2013-10-10 11:17 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:20:40 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的情况下使用此包,你可以使用以下两行代码来启动已为此包配置的自动加载器

<?php

use SyHolloway\MrColor\Color;

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