php: RGB to HSB(HSV)

Preface

        Recently, because it involves a color calculation rule, which requires the conversion of RGB to HSB, I studied it myself, so I recorded it.

What is RGB?

        RGB stands for red (R), green (G), and blue (B), also known as the three primary colors of light. It obtains a variety of colors by changing three color channels and superimposing them on each other. RGB represents the colors of the three channels of red, green, and blue. This standard covers almost all human vision requirements. All colors that can be perceived are one of the most widely used color systems currently.

What is HSB?

        In HSB mode, H (hues) represents hue, S (saturation) represents saturation, and B (brightness) represents brightness. The medium corresponding to the HSB mode is the human eye.

Now that we have RGB, why do we need HSB?

        We know that the colors that the human eye can see can be obtained by mixing the three primary colors RGB, and each of its color components is red (R), green (G), and blue (B). However, this is just a representation method of color. This representation method is very suitable for machines but very anti-human. When people pick up colors, the thinking process they usually have in mind is:

what colour? Bright or not? Light or dark?

Therefore, in order to be more consistent with people's thinking process in the process of picking colors, HSB was born. HSB is just another representation method of RGB color space, so the size of the color space represented by the two is the same.

In addition, HSB is also called HSV. Both refer to the same thing. The reason why there are two names may be to avoid thinking that the letter B in HSB and the letter B in RGB have the same meaning in some contexts. Bar!

RGB conversion HSB formula

 

Note: max is the largest of the three color components of RGB. Similarly, min means the smallest of the three color components of RGB. Because HSB is equivalent to HSV, in order to distinguish it from B in RGB, In the formula, the letter V is equivalent to the B in HSB.

According to the above formula:

DEMO (PHP)

<?php

/**
 * RGB转换HSB(HSV)
 * RGB input  values: 0-255, 0-255, 0-255
 * HSB output values: 0-360, 0-100, 0-100
 * 
 * @param  int $red   
 * @param  int $green 
 * @param  int $blue
 * @return array
 */
function rgbToHsb($red, $green, $blue) { 
    $rgb = array($red, $green, $blue);
    $max = max($rgb);
    $min = min($rgb);
    $diff = $max - $min;

    /* 计算色相 */
    $hue = 0;
    if ($max == $min) {
        $hue = 0;
    } else if ($max == $red && $green >= $blue) {
        $hue = 60 * (($green - $blue) / $diff);
    } else if ($max == $red && $green < $blue) {
        $hue = 60 * (($green - $blue) / $diff) + 360;
    } else if ($max == $green) {
        $hue = 60 * (($blue - $red) / $diff) + 120;
    } else if ($max == $blue) {
        $hue = 60 * (($red - $green) / $diff) + 240;
    }
    
    /* 计算饱和度 */
    if ($max == 0) {
        $saturation = 0;
    } else {
        $saturation = (1 - $min / $max) * 100;
    }

    /* 计算色调 */
    $value = $max / 255 * 100; 
    return [$hue, $saturation, $value];
}

DEMO (JAVA)

	public static float[] rgbToHsb(int rgbR, int rgbG, int rgbB) {
		assert 0 <= rgbR && rgbR <= 255;
		assert 0 <= rgbG && rgbG <= 255;
		assert 0 <= rgbB && rgbB <= 255;
		int[] rgb = new int[] { rgbR, rgbG, rgbB };
		Arrays.sort(rgb);
		int max = rgb[2];
		int min = rgb[0];
 
		float hsbB = max / 255.0f * 100f;
		float hsbS = max == 0 ? 0 : (max - min) / (float) max * 100f;
 
		float hsbH = 0;
		if (max == rgbR && rgbG >= rgbB) {
			hsbH = (rgbG - rgbB) * 60f / (max - min);
		} else if (max == rgbR && rgbG < rgbB) {
			hsbH = (rgbG - rgbB) * 60f / (max - min) + 360;
		} else if (max == rgbG) {
			hsbH = (rgbB - rgbR) * 60f / (max - min) + 120;
		} else if (max == rgbB) {
			hsbH = (rgbR - rgbG) * 60f / (max - min) + 240;
		}
 
		return new float[] { hsbH, hsbS, hsbB };
	}

Guess you like

Origin blog.csdn.net/panjiapengfly/article/details/123442225