Several common problems in GDI + (8)

Source: Internet
Author: User

Haha, this week, the eighth episode of Heroes finally came out, I continue to go down the last section of the story, modify the color how to do.

8.1 Using the original method to modify the RGB and hue, saturation and brightness of the image

The simplest way, very easy, is to use the previous lockbits, and then directly modify the value of the r,g,b, the specific is not much to say. In GDI +, color has 3 methods, namely GetHue (), Getsaturation (), getbrightness (). It is the hue, saturation and brightness of the image. Where the hue value is [0,360], which indicates the current color at which angle, saturation and brightness are from [0,1], indicating the saturation and brightness of the color. Here, GDI + 's comments make another mistake, such as Getsaturation (): Gets the hue-saturation-brightness (HSB) saturation value for this Color structure. , saying that he is HSB, but in fact its value is the result of HSL. The person who wrote the note should pull out for a beating and not know how much time I wasted on it.

HSB also known as HSV, is also a very common color space, which is based on the separation of brightness information and color information, widely used in various computer image software. Another color space called HSL, similar to the HSV, but in the brightness and saturation of the treatment slightly different, interested students can go to check this article Http://en.wikipedia.org/wiki/HSL_color_space, There are a lot of beautiful pictures and detailed comparisons, I do not say more here. Here's a look at the HSV color space model:

To get to the point, if I want to modify the saturation of an image, how do I do it? GDI + did a half-baked thing, color inside there is a static method called FromArgb, but there is no way to call FROMHSL (float hue, float saturation, float lightness). So everyone is unhappy. Here I change the brightness to lightness to avoid errors in the annotation.

1 Private Color FROMHSL (float hue, float saturation, float lightness)
2 {
3 Double q = lightness < 0.5d? Lightness * (1 + saturation): Lightness + Saturation-(lightness * saturation);
4 Double p = 2 * LIGHTNESS-Q;
5 Double HK = hue/360d;             
6
7 double[] t = new double[3];//save RGB in array
8 t[0] = HK + 1/3d;
9 T[1] = HK;
Ten t[2] = hk-1/3d;
One
for (int i = 0; i < 3; i++)
{
if (T[i] < 0) T [i] + 1d;
if (T[i] > 1) t[i] = 1d;
0}

double[] color = new DOUBLE[3];
for (int i =; ; 3;                      i++)
{
if (T[i] < 1/6d)
{
23 Color[i] = p + ((q-p) * 6 * t[i]);        
24          
/Else if (T[i] < 1/2d)
{
color[ I] = q;                     
}
Else if (T[i] < 2/3d)
{
31 Color[i] = p + ((q-p) * 6 * (2/3d-t[i)));
32}
-Else
36}< {
Color[i] = p;
 br>

Color.FromArgb (0,
(int) math.round (color[0) * 255d),
(int) math.round (color[1] * 255d),
(int) math.round (color[2] * 255d ));
[
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.