简体   繁体   中英

Get Color Shade with PHP

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.将颜色分为三个元素:cc,66、99 2.使用http://php.net/manual/de/function.hexdec.php将其转换为十进制3.递增三个十进制值4.将十进制转换为十六进制再次5.将三个要素放在一起

与您的问题没有直接关系,但是这篇文章对颜色转换很有趣: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM