PHP 函数需要帮助:brightness();使 RGB 颜色更暗/更亮

发布于 2024-10-20 15:52:33 字数 2200 浏览 0 评论 0原文

想象一个十六进制的有效 #RGB 颜色,定义为 $color = "#f7b9a0";

现在我想让 php 从这个 $color 派生出另外两种稍浅/稍深的颜色(相同的色调/颜色但只是改变了亮度)。我可以通过哪些方式实现这一目标?什么代码会生成这个?我感觉我需要一些简单的东西,比如:

brightness(input rgb color, ± number of steps); // function outputs the new RGB
 // ?? What php code should go here??

理想情况下,我希望在我的 html 中有这样的东西:

.classDefault {color:<?=$color?> }
.classLighter {color:<?=brightness($color,+10)?> } /* 10 steps brighter */
.classDarker  {color:<?=brightness($color,-25)?> } /* 25 steps darker   */

brightness(); 函数中应该包含什么 PHP 代码?为了我的梦想成真?
任何建议和/或代码都非常感谢!


从以下答案更新:

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);
  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = max(0,min(255,$r + $steps));
  $g = max(0,min(255,$g + $steps));  
  $b = max(0,min(255,$b + $steps));

  return '#'.dechex($r).dechex($g).dechex($b);
}

### NOW LETS DEFINE MY COLOR
$color = "#2233FF";

### DERIVED BRIGHTER COLORS
$color1 = brightness($color,25);
$color2 = brightness($color,50);
$color3 = brightness($color,75);

### DERIVED DARKER COLORS
$color4 = brightness($color,-25);
$color5 = brightness($color,-50);
$color6 = brightness($color,-75);


<!-- BRIGHTER -->
<div style=" background-color:<?=$color3?>"><?=$color3?></div>
<div style=" background-color:<?=$color2?>"><?=$color2?></div>
<div style=" background-color:<?=$color1?>"><?=$color1?></div>

<!-- DEFINED CONSTANT -->
<div style=" background-color:<?=$color?>"><?=$color?></div>

<!-- DARKER -->
<div style=" background-color:<?=$color4?>"><?=$color4?></div>
<div style=" background-color:<?=$color5?>"><?=$color5?></div>
<div style=" background-color:<?=$color6?>"><?=$color6?></div>

较亮的颜色有效,但较暗的颜色无效。哦,好吧,一半解决方案至少是解决方案的很大一部分,所以非常感谢!

Imagine a valid #RGB color in hexadecimal, defined as $color = "#f7b9a0";

Now I want to have php derive from this $color two other colors which are slightly lighter/darker (same hue/color but just altered brightness). What ways could I achieve this? What code will generate this? I have the feeling I need something simple like:

brightness(input rgb color, ± number of steps); // function outputs the new RGB
 // ?? What php code should go here??

Ideally, I wish to have something like this in my html:

.classDefault {color:<?=$color?> }
.classLighter {color:<?=brightness($color,+10)?> } /* 10 steps brighter */
.classDarker  {color:<?=brightness($color,-25)?> } /* 25 steps darker   */

What PHP code should go in the brightness(); function? for my dream to come true?
Any suggestions and or code are both very appreciated!


updated from the answer below:

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);
  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = max(0,min(255,$r + $steps));
  $g = max(0,min(255,$g + $steps));  
  $b = max(0,min(255,$b + $steps));

  return '#'.dechex($r).dechex($g).dechex($b);
}

### NOW LETS DEFINE MY COLOR
$color = "#2233FF";

### DERIVED BRIGHTER COLORS
$color1 = brightness($color,25);
$color2 = brightness($color,50);
$color3 = brightness($color,75);

### DERIVED DARKER COLORS
$color4 = brightness($color,-25);
$color5 = brightness($color,-50);
$color6 = brightness($color,-75);


<!-- BRIGHTER -->
<div style=" background-color:<?=$color3?>"><?=$color3?></div>
<div style=" background-color:<?=$color2?>"><?=$color2?></div>
<div style=" background-color:<?=$color1?>"><?=$color1?></div>

<!-- DEFINED CONSTANT -->
<div style=" background-color:<?=$color?>"><?=$color?></div>

<!-- DARKER -->
<div style=" background-color:<?=$color4?>"><?=$color4?></div>
<div style=" background-color:<?=$color5?>"><?=$color5?></div>
<div style=" background-color:<?=$color6?>"><?=$color6?></div>

the brighter colors work, but the darker not. Oh well half solution is at least big part of solution so Thanks very much!

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

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

发布评论

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

评论(5

云胡 2024-10-27 15:52:33

沿着这些思路...

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);
  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = max(0,min(255,$r + $steps));
  $g = max(0,min(255,$g + $steps));  
  $b = max(0,min(255,$b + $steps));

  return '#'.dechex($r).dechex($g).dechex($b);
}

调用 $colour = alter_brightness('#2233FF',5);

Something along these lines...

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);
  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = max(0,min(255,$r + $steps));
  $g = max(0,min(255,$g + $steps));  
  $b = max(0,min(255,$b + $steps));

  return '#'.dechex($r).dechex($g).dechex($b);
}

Call like $colour = alter_brightness('#2233FF',5);

晒暮凉 2024-10-27 15:52:33

Cintia 几乎是正确的,但是 str_pad 应该在之前而不是之后添加 0:

<?php 

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);

  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = dechex(max(0,min(255,$r + $steps)));
  $g = dechex(max(0,min(255,$g + $steps)));  
  $b = dechex(max(0,min(255,$b + $steps)));

  $r = str_pad($r,2,"0",STR_PAD_LEFT);
  $g = str_pad($g,2,"0",STR_PAD_LEFT);
  $b = str_pad($b,2,"0",STR_PAD_LEFT);

  $cor = '#'.$r.$g.$b;

  return $cor;
}

?>

Cintia is almost right, but the str_pad should add a 0 before not after:

<?php 

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);

  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = dechex(max(0,min(255,$r + $steps)));
  $g = dechex(max(0,min(255,$g + $steps)));  
  $b = dechex(max(0,min(255,$b + $steps)));

  $r = str_pad($r,2,"0",STR_PAD_LEFT);
  $g = str_pad($g,2,"0",STR_PAD_LEFT);
  $b = str_pad($b,2,"0",STR_PAD_LEFT);

  $cor = '#'.$r.$g.$b;

  return $cor;
}

?>
怪异←思 2024-10-27 15:52:33
<?php 

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);

  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = dechex(max(0,min(255,$r + $steps)));
  $g = dechex(max(0,min(255,$g + $steps)));  
  $b = dechex(max(0,min(255,$b + $steps)));

  $r = str_pad($r,2,"0");
  $g = str_pad($g,2,"0");
  $b = str_pad($b,2,"0");

  $cor = '#'.$r.$g.$b;

  return $cor;
}

?>
<?php 

function alter_brightness($colourstr, $steps) {
  $colourstr = str_replace('#','',$colourstr);

  $rhex = substr($colourstr,0,2);
  $ghex = substr($colourstr,2,2);
  $bhex = substr($colourstr,4,2);

  $r = hexdec($rhex);
  $g = hexdec($ghex);
  $b = hexdec($bhex);

  $r = dechex(max(0,min(255,$r + $steps)));
  $g = dechex(max(0,min(255,$g + $steps)));  
  $b = dechex(max(0,min(255,$b + $steps)));

  $r = str_pad($r,2,"0");
  $g = str_pad($g,2,"0");
  $b = str_pad($b,2,"0");

  $cor = '#'.$r.$g.$b;

  return $cor;
}

?>
眼睛会笑 2024-10-27 15:52:33

使用第二个答案。或将以下内容添加到您的代码中:

如果 rgb 值为 10 左右,它将返回十六进制的单个字符。它需要以 0 为前缀才能正确渲染。

  $newhex = '#';
  $newhex .= (strlen(dechex($r)) === 1) ?  '0'.dechex($r) : dechex($r);
  $newhex .= (strlen(dechex($g)) === 1) ?  '0'.dechex($g) : dechex($g);
  $newhex .= (strlen(dechex($b)) === 1) ?  '0'.dechex($b) : dechex($b);

  return $newhex;

Use the second answer. or add the following to your code:

If the rgb value is 10 or so, it will return single character for hex. It needs to be prefixed with 0 to render properly.

  $newhex = '#';
  $newhex .= (strlen(dechex($r)) === 1) ?  '0'.dechex($r) : dechex($r);
  $newhex .= (strlen(dechex($g)) === 1) ?  '0'.dechex($g) : dechex($g);
  $newhex .= (strlen(dechex($b)) === 1) ?  '0'.dechex($b) : dechex($b);

  return $newhex;
往事风中埋 2024-10-27 15:52:33

这是该函数的缩小版本。我使用 str_pad 为数字 添加 0 10. cusimar9版本不检查这一点。

 function alter_brightness($colourstr, $steps) {
    //Take off the #
    $colourstr    = str_replace( '#', '', $colourstr );
    // Steps should be between -255 and 255. Negative = darker, positive = lighter
    $steps  = max( -255, min( 255, $steps ) );
    // Transform colors of type #fff to #ffffff
    if ( 3 == strlen( $colourstr ) ) {
        $colourstr    = str_repeat( substr( $colourstr, 0, 1 ), 2 ) . str_repeat( substr( $colourstr, 1, 1 ), 2 ) . str_repeat( substr( $colourstr, 2, 1 ), 2 );
    }
    // Modify the brigthness of each component
    $rgb=array(substr($colourstr,0,2),  substr($colourstr,2,2), substr($colourstr,4,2));
    for($i = 0; $i< count($rgb); $i++){
      $rgb[$i] = str_pad(dechex(max(0,min(255, hexdec($rgb[$i]) + $steps))),2,"0",STR_PAD_LEFT) ;
    }
    return '#'.implode('', $rgb);
}

Here is the minified version of the function. I used str_pad to add the 0 for the numbers < 10. The version of cusimar9 doesn't check that.

 function alter_brightness($colourstr, $steps) {
    //Take off the #
    $colourstr    = str_replace( '#', '', $colourstr );
    // Steps should be between -255 and 255. Negative = darker, positive = lighter
    $steps  = max( -255, min( 255, $steps ) );
    // Transform colors of type #fff to #ffffff
    if ( 3 == strlen( $colourstr ) ) {
        $colourstr    = str_repeat( substr( $colourstr, 0, 1 ), 2 ) . str_repeat( substr( $colourstr, 1, 1 ), 2 ) . str_repeat( substr( $colourstr, 2, 1 ), 2 );
    }
    // Modify the brigthness of each component
    $rgb=array(substr($colourstr,0,2),  substr($colourstr,2,2), substr($colourstr,4,2));
    for($i = 0; $i< count($rgb); $i++){
      $rgb[$i] = str_pad(dechex(max(0,min(255, hexdec($rgb[$i]) + $steps))),2,"0",STR_PAD_LEFT) ;
    }
    return '#'.implode('', $rgb);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文