开发者

Get Color Shade with PHP

开发者 https://www.devze.com 2023-02-18 09:16 出处:网络
I want to get brighter hex colour shade output from a given hex value with PHP. For example, I give the colour #cc6699 as input, and I want #ee88aa as the output colour. How would I go about 开发者_运

I want to get brighter hex colour shade output from a given hex value with PHP. For example, I give the colour #cc6699 as input, and I want #ee88aa as the output colour. How would I go about 开发者_运维百科doing this in PHP?


You need to convert the color to RGB, make the additions, and convert back:

// Convert string to 3 decimal values (0-255)
$rgb = array_map('hexdec', str_split("cc6699", 2));

// Modify color
$rgb[0] += 34;
$rgb[1] += 34;
$rgb[2] += 17;

// Convert back
$result = implode('', array_map('dechex', $rgb));

echo $result;

See it in action here.


1. split the color in three elements: cc, 66, 99 2. Convert it to decimal with http://php.net/manual/de/function.hexdec.php 3. Increment three decimal values 4. Convert decimal to hex again 5. put the three elements together


Not directly related to your question, but this article is interesting for color transformation : http://beesbuzz.biz/code/hsv_color_transforms.php


The best solution is to convert the RGB to HSL or HSV (just search google for php converter hsl/hsv).

Then you can play with the 'lightness' or 'value' values of the colorspace.

Afterwards convert it back to RGB colorspace.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号