PHP Hsv to Rgb Formula Comprehension

PHP HSV to RGB formula comprehension

This is for the the HSV values in the range [0,1] (and giving RGB values in the range [0,1], instead of {0, 1, ..., 255}:

function HSVtoRGB(array $hsv) {
list($H,$S,$V) = $hsv;
//1
$H *= 6;
//2
$I = floor($H);
$F = $H - $I;
//3
$M = $V * (1 - $S);
$N = $V * (1 - $S * $F);
$K = $V * (1 - $S * (1 - $F));
//4
switch ($I) {
case 0:
list($R,$G,$B) = array($V,$K,$M);
break;
case 1:
list($R,$G,$B) = array($N,$V,$M);
break;
case 2:
list($R,$G,$B) = array($M,$V,$K);
break;
case 3:
list($R,$G,$B) = array($M,$N,$V);
break;
case 4:
list($R,$G,$B) = array($K,$M,$V);
break;
case 5:
case 6: //for when $H=1 is given
list($R,$G,$B) = array($V,$M,$N);
break;
}
return array($R, $G, $B);
}

PHP function to convert HSL to RGB or Hex

Taking the code from one of the answers in the link of Jim's comment (PHP HSV to RGB formula comprehension), we can compute it as follows:

<?php    
$hue = 209;
$sat = 75;
$lum = 60;

$hue /= 360;
$sat /= 100;
$lum /= 100;

$result = ColorHSLToRGB($hue, $sat, $lum);
var_dump($result); echo '<br>';
printf("rgb = %d,%d,%d<br>", $result['r'], $result['g'], $result['b']);

function ColorHSLToRGB($h, $s, $l){

$r = $l;
$g = $l;
$b = $l;
$v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s);
if ($v > 0){
$m;
$sv;
$sextant;
$fract;
$vsf;
$mid1;
$mid2;

$m = $l + $l - $v;
$sv = ($v - $m ) / $v;
$h *= 6.0;
$sextant = floor($h);
$fract = $h - $sextant;
$vsf = $v * $sv * $fract;
$mid1 = $m + $vsf;
$mid2 = $v - $vsf;

switch ($sextant)
{
case 0:
$r = $v;
$g = $mid1;
$b = $m;
break;
case 1:
$r = $mid2;
$g = $v;
$b = $m;
break;
case 2:
$r = $m;
$g = $v;
$b = $mid1;
break;
case 3:
$r = $m;
$g = $mid2;
$b = $v;
break;
case 4:
$r = $mid1;
$g = $m;
$b = $v;
break;
case 5:
$r = $v;
$g = $m;
$b = $mid2;
break;
}
}
return array('r' => $r * 255.0, 'g' => $g * 255.0, 'b' => $b * 255.0);
}
?>

Output:

array(3) { ["r"]=> float(76.5) ["g"]=> float(155.55) ["b"]=> float(229.5) } 
rgb = 76,155,229

How to convert artbitrary number to rgb color?

I don't have a solution, per se, but a suggestion.

First, since you want each user's colors to be unique but random, you could get started with some kind of numerical hash function. That gets you a random number that's most likely unique. if your hash function is decent and your users are in the hundreds that should suffice.

Now you need to turn that into colors, preferably on the dark end of the spectrum. If you go with RGB, You need three numbers. To keep them dark, lets say you keep the total or R, G and B under 200. That's a guess, of course, but it should be pretty dark. Pick a random number [index] between 1 and 3 - that's the R, G or B you're going to start with. Get a number between 0 and 200, and set record it for color[index1]. Now pick one of the remaining indexes, 1-3. For this, pick a number between 0 and color[index1] that you already have. Set the remaining number to 200-color[index1]-color[index2]. Now you have a randomish RGB value that should contrast with white.

I realize I never got much use from that hash function up front - maybe ditch it and just use the userid as a random(seed).

Altering Saturation of #RGB Color: Whats Missing Here?

Saturation and brightness cannot be handled the same (one could argue that your aren't handling brightness correctly using this code but it's probably close enough). See this question RGB to HSV in PHP for how to convert the color to an HSV value then you can modify the saturation (the S value). Then convert back using the answer to this question PHP HSV to RGB.

Convert HSV to RGB in MATLAB

Using the in-built hsv2rgb function...

% Some colour in HSV, [Hue (0-360), Saturation (0-1), Value (0-1)]
myHSV = [217, 0.4, 0.72];
% hsv2rgb takes Hue value in range 0-1, so...
myHSV(1) = myHSV(1) / 360;
% Convert to RGB with values in range (0-1)
myRGBpct = hsv2rgb(myHSV);
% Convert to RGB with values in range (0-255)
myRGB255 = myRGBpct * 255;

Putting all of this together, we can simply do

myHSV = [217, 0.4, 0.72];
myRGB255 = hsv2rgb(myHSV ./ [360, 1, 1]) * 255;
>> myRGB255 = [110.16, 138.31, 183.60]

Testing this using Google's color picker, we can see this is the correct solution. If you wanted to do any other RGB manipulation within MATLAB then leave the values in the range (0-1), since that is what MATLAB always uses.

blue

If you have many HSV values, store them in an mx3 matrix, with columns H,S and V. Then similarly to the above you can do:

myHSV = [217, 0.4, 0.72;
250, 0.5, 0.2;
% ... more rows
];
myHSV(:,1) = myHSV(:,1) / 360;
myRGB255 = hsv2rgb(myHSV) * 255;

HSL to RGB color conversion

Found the easiest way, python to the rescue :D

colorsys.hls_to_rgb(h, l, s)

Convert the color from HLS coordinates to RGB coordinates.

Percentage to Hexcolor in PHP

This little snippet would appear to do what you're trying to achieve

http://bytes.com/topic/php/insights/890539-how-produce-first-pair-rgb-hex-color-value-percent-value

function percent2Color($value,$brightness = 255, $max = 100,$min = 0, $thirdColorHex = '00')
{
// Calculate first and second color (Inverse relationship)
$first = (1-($value/$max))*$brightness;
$second = ($value/$max)*$brightness;

// Find the influence of the middle color (yellow if 1st and 2nd are red and green)
$diff = abs($first-$second);
$influence = ($brightness-$diff)/2;
$first = intval($first + $influence);
$second = intval($second + $influence);

// Convert to HEX, format and return
$firstHex = str_pad(dechex($first),2,0,STR_PAD_LEFT);
$secondHex = str_pad(dechex($second),2,0,STR_PAD_LEFT);

return $firstHex . $secondHex . $thirdColorHex ;

// alternatives:
// return $thirdColorHex . $firstHex . $secondHex;
// return $firstHex . $thirdColorHex . $secondHex;

}

Paint me a Rainbow

Use HSL instead: fix the brightness and saturation and vary the hue from 0 to 360, then convert to RGB.

HSL describes colors as they are perceived by people. RGB describes them as they are used by machines. So you can't really do anything visually pleasing directly using RGB.



Related Topics



Leave a reply



Submit