如何使用 PHP 制作颜色的浅色版本?

发布于 2024-10-18 20:16:29 字数 588 浏览 10 评论 0 原文

地球同胞们大家好。关于 RGB 颜色及其在简单的小型 php 代码中的有用性的问题:

假设我有变量 $colorA 包含有效的六字符颜色。比如说B1B100,一种绿色的自然色。现在,如果我想从中创建一种新颜色,比方说,比原始颜色大约浅十步。

$colorA = B1B100 // original color
php code with little color engine lightening stuff up goes here
$colorB = ?????? // original color lightened up

是否有一个 PHP 就绪函数可以知道 RGB 颜色,例如

php 函数 RGB(输入颜色,做什么,输出颜色) 哪里可以做 +/- 255 亮度值等。

这样的事情已经可能发生还是我在做白日梦?

rgb-hsl($colorA, +10, $colorB);

如果这不存在,那么执行此操作的最短代码是什么?建议、代码或想法都是我的答案。谢谢。

Hello fellow earthlings. A quesion about RGB color and its usefulness in a simple tiny php code:

Imagine I have variable $colorA containning a valid six char color. say B1B100, a greenish natural color. Now If I would like to make a new color from that, which is, say, ten steps lighter thatn that original color, roughly.

$colorA = B1B100 // original color
php code with little color engine lightening stuff up goes here
$colorB = ?????? // original color lightened up

Is there a php ready function that KNOWS rgb colors something like

php function RGB ( input color, what to do, output color)
Where what to do could be +/- 255 values of brightness etc etc.

Is something like this already possible or am I day dreaming?

rgb-hsl($colorA, +10, $colorB);

If this does not exist, what would be the shortest code for doing this? Suggestions, code or ideas are all answers to me. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦纸 2024-10-25 20:16:29

这个问题有一个完整的PHP脚本可以将 RGB 转换为 HSL 颜色,并增加 HSL 颜色的 H 分量 - 改为增加 L 应该很简单。

This SO question has a full-blown PHP script that can convert a RGB to a HSL colour, and increase its H component of a HSL colour - it should be trivial to change to increase L instead.

勿挽旧人 2024-10-25 20:16:29

一般来说,如果您想要特定颜色的较浅色调,最准确的过程是从 RGB 转换为 HSL(或 HSV),更改代表亮度的“L”(或“V”)值,然后转换回RGB。

这将保留“色调”,它代表颜色在光谱上的位置,但会更改该颜色的“色调”(如果变亮)或“阴影”(如果变暗)。

有关详细信息,请参阅 http://en.wikipedia.org/wiki/HSL_and_HSV

In general if you want a lighter shade of a particular colour, the most accurate process is to convert from RGB to HSL (or HSV), change the 'L' (or 'V') value which represents lightness, and then convert back to RGB.

This will preserve the "hue", which represents where the colour sits on the spectrum, but change the "tint" (if lightening) or "shade" (if darkening) of that colour.

See http://en.wikipedia.org/wiki/HSL_and_HSV for more information.

我是男神闪亮亮 2024-10-25 20:16:29

在此网站上: http://www.sitepoint.com/forums/showthread.php ?t=586223 他们正在谈论这个最初由开源 Drupal 编写的代码。似乎在 PHP 中工作得很好!?

现在,我如何在再次将其输出为 RGB 之前将自己与此代码混合并更改 HSL 值的亮度?

<?php
### RGB >> HSL
function _color_rgb2hsl($rgb) {
  $r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2];
  $min = min($r, min($g, $b)); $max = max($r, max($g, $b));
  $delta = $max - $min; $l = ($min + $max) / 2; $s = 0;
  if ($l > 0 && $l < 1) {
    $s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
  }
  $h = 0;
  if ($delta > 0) {
    if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
    if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
    if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
    $h /= 6;
  } return array($h, $s, $l);
}

### HSL >> RGB
function _color_hsl2rgb($hsl) {
  $h = $hsl[0]; $s = $hsl[1]; $l = $hsl[2];
  $m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
  $m1 = $l * 2 - $m2;
  return array(_color_hue2rgb($m1, $m2, $h + 0.33333),
               _color_hue2rgb($m1, $m2, $h),
               _color_hue2rgb($m1, $m2, $h - 0.33333));
}

### Helper function for _color_hsl2rgb().
function _color_hue2rgb($m1, $m2, $h) {
  $h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
  if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
  if ($h * 2 < 1) return $m2;
  if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
  return $m1;
}

### Convert a hex color into an RGB triplet.
function _color_unpack($hex, $normalize = false) {
  if (strlen($hex) == 4) {
    $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
  } $c = hexdec($hex);
  for ($i = 16; $i >= 0; $i -= 8) {
    $out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
  } return $out;
}

### Convert an RGB triplet to a hex color.
function _color_pack($rgb, $normalize = false) {
  foreach ($rgb as $k => $v) {
    $out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
  }return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
}

/* $testrgb = array(0.2,0.75,0.4); //RGB to start with
print_r($testrgb); */
      print "Hex: ";

  $testhex = "#b7b700";
      print $testhex;

  $testhex2rgb = _color_unpack($testhex,true); 
      print "<br />RGB: ";

  var_dump($testhex2rgb);
      print "<br />HSL color module: ";

  $testrgb2hsl = _color_rgb2hsl($testhex2rgb); //Converteren naar HSL

  var_dump($testrgb2hsl);
      print "<br />RGB: ";

  $testhsl2rgb = _color_hsl2rgb($testrgb2hsl); // En weer terug naar RGB    
  var_dump($testhsl2rgb); 
      print "<br />Hex: ";

  $testrgb2hex = _color_pack($testhsl2rgb,true);
  var_dump($testrgb2hex);

?>

On this website: http://www.sitepoint.com/forums/showthread.php?t=586223 they are talking about this code which is originally made by opensource Drupal. Seems to work fine in PHP!?

Now, how do I now indermingle myself with this code and change the lightness of an HSL value, before its outputted as RGB again?

<?php
### RGB >> HSL
function _color_rgb2hsl($rgb) {
  $r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2];
  $min = min($r, min($g, $b)); $max = max($r, max($g, $b));
  $delta = $max - $min; $l = ($min + $max) / 2; $s = 0;
  if ($l > 0 && $l < 1) {
    $s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
  }
  $h = 0;
  if ($delta > 0) {
    if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
    if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
    if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
    $h /= 6;
  } return array($h, $s, $l);
}

### HSL >> RGB
function _color_hsl2rgb($hsl) {
  $h = $hsl[0]; $s = $hsl[1]; $l = $hsl[2];
  $m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
  $m1 = $l * 2 - $m2;
  return array(_color_hue2rgb($m1, $m2, $h + 0.33333),
               _color_hue2rgb($m1, $m2, $h),
               _color_hue2rgb($m1, $m2, $h - 0.33333));
}

### Helper function for _color_hsl2rgb().
function _color_hue2rgb($m1, $m2, $h) {
  $h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
  if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
  if ($h * 2 < 1) return $m2;
  if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
  return $m1;
}

### Convert a hex color into an RGB triplet.
function _color_unpack($hex, $normalize = false) {
  if (strlen($hex) == 4) {
    $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
  } $c = hexdec($hex);
  for ($i = 16; $i >= 0; $i -= 8) {
    $out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
  } return $out;
}

### Convert an RGB triplet to a hex color.
function _color_pack($rgb, $normalize = false) {
  foreach ($rgb as $k => $v) {
    $out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
  }return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
}

/* $testrgb = array(0.2,0.75,0.4); //RGB to start with
print_r($testrgb); */
      print "Hex: ";

  $testhex = "#b7b700";
      print $testhex;

  $testhex2rgb = _color_unpack($testhex,true); 
      print "<br />RGB: ";

  var_dump($testhex2rgb);
      print "<br />HSL color module: ";

  $testrgb2hsl = _color_rgb2hsl($testhex2rgb); //Converteren naar HSL

  var_dump($testrgb2hsl);
      print "<br />RGB: ";

  $testhsl2rgb = _color_hsl2rgb($testrgb2hsl); // En weer terug naar RGB    
  var_dump($testhsl2rgb); 
      print "<br />Hex: ";

  $testrgb2hex = _color_pack($testhsl2rgb,true);
  var_dump($testrgb2hex);

?>
唐婉 2024-10-25 20:16:29

PHP 确实有几个图像处理库。 GDImagemagick

编辑:我操之过急,这些库没有直接的 PHP 颜色操作函数 - 老实说,在看到它们可以对图像执行很多操作后,我真的认为它们做了某种操作通过 PHP。他们确实完成了很多很酷的事情。 这是一个人的例子。

PHP does have a couple image manipulation libraries. Either GD or Imagemagick

EDIT: I jumped the gun, these libraries do not have direct PHP color manipulation functions - I honestly assumed they did of a sort after seeing a lot of the things they can do with images via PHP. They do accomplish a lot of cool things. Here's one guy's example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文