Esempio n. 1
0
    /// <summary>
    /// Convert from RGB to YCbCr color space (Rec 601-1 specification). 
    /// </summary>
    /// 
    /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
    /// <param name="ycbcr">Destination color in <b>YCbCr</b> color space.</param>
    /// 
    public static void FromRGB(RGB rgb, YCbCr ycbcr)
    {
      float r = (float)rgb.Red / 255;
      float g = (float)rgb.Green / 255;
      float b = (float)rgb.Blue / 255;

      ycbcr.Y = (float)(0.2989 * r + 0.5866 * g + 0.1145 * b);
      ycbcr.Cb = (float)(-0.1687 * r - 0.3313 * g + 0.5000 * b);
      ycbcr.Cr = (float)(0.5000 * r - 0.4184 * g - 0.0816 * b);
    }
Esempio n. 2
0
    /// <summary>
    /// Convert from RGB to HSL color space.
    /// </summary>
    /// 
    /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
    /// <param name="hsl">Destination color in <b>HSL</b> color space.</param>
    /// 
    /// <remarks><para>See <a href="http://en.wikipedia.org/wiki/HSI_color_space#Conversion_from_RGB_to_HSL_or_HSV">HSL and HSV Wiki</a>
    /// for information about the algorithm to convert from RGB to HSL.</para></remarks>
    /// 
    public static void FromRGB(RGB rgb, HSL hsl)
    {
      float r = (rgb.Red / 255.0f);
      float g = (rgb.Green / 255.0f);
      float b = (rgb.Blue / 255.0f);

      float min = Math.Min(Math.Min(r, g), b);
      float max = Math.Max(Math.Max(r, g), b);
      float delta = max - min;

      // get luminance value
      hsl.Luminance = (max + min) / 2;

      if (delta == 0)
      {
        // gray color
        hsl.Hue = 0;
        hsl.Saturation = 0.0f;
      }
      else
      {
        // get saturation value
        hsl.Saturation = (hsl.Luminance <= 0.5) ? (delta / (max + min)) : (delta / (2 - max - min));

        // get hue value
        float hue;

        if (r == max)
        {
          hue = ((g - b) / 6) / delta;
        }
        else if (g == max)
        {
          hue = (1.0f / 3) + ((b - r) / 6) / delta;
        }
        else
        {
          hue = (2.0f / 3) + ((r - g) / 6) / delta;
        }

        // correct hue if needed
        if (hue < 0)
          hue += 1;
        if (hue > 1)
          hue -= 1;

        hsl.Hue = (int)(hue * 360);
      }
    }
Esempio n. 3
0
 /// <summary>
 /// Convert the color to <b>RGB</b> color space.
 /// </summary>
 /// 
 /// <returns>Returns <see cref="RGB"/> instance, which represents converted color value.</returns>
 /// 
 public RGB ToRGB()
 {
   RGB rgb = new RGB();
   ToRGB(this, rgb);
   return rgb;
 }
Esempio n. 4
0
    /// <summary>
    /// Convert from HSL to RGB color space.
    /// </summary>
    /// 
    /// <param name="hsl">Source color in <b>HSL</b> color space.</param>
    /// <param name="rgb">Destination color in <b>RGB</b> color space.</param>
    /// 
    public static void ToRGB(HSL hsl, RGB rgb)
    {
      if (hsl.Saturation == 0)
      {
        // gray values
        rgb.Red = rgb.Green = rgb.Blue = (byte)(hsl.Luminance * 255);
      }
      else
      {
        float v1, v2;
        float hue = (float)hsl.Hue / 360;

        v2 = (hsl.Luminance < 0.5) ?
            (hsl.Luminance * (1 + hsl.Saturation)) :
            ((hsl.Luminance + hsl.Saturation) - (hsl.Luminance * hsl.Saturation));
        v1 = 2 * hsl.Luminance - v2;

        rgb.Red = (byte)(255 * Hue_2_RGB(v1, v2, hue + (1.0f / 3)));
        rgb.Green = (byte)(255 * Hue_2_RGB(v1, v2, hue));
        rgb.Blue = (byte)(255 * Hue_2_RGB(v1, v2, hue - (1.0f / 3)));
      }
    }
Esempio n. 5
0
 /// <summary>
 /// Convert from RGB to HSL color space.
 /// </summary>
 /// 
 /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
 /// 
 /// <returns>Returns <see cref="HSL"/> instance, which represents converted color value.</returns>
 /// 
 public static HSL FromRGB(RGB rgb)
 {
   HSL hsl = new HSL();
   FromRGB(rgb, hsl);
   return hsl;
 }
Esempio n. 6
0
 /// <summary>
 /// Convert from RGB to YCbCr color space (Rec 601-1 specification).
 /// </summary>
 /// 
 /// <param name="rgb">Source color in <b>RGB</b> color space.</param>
 /// 
 /// <returns>Returns <see cref="YCbCr"/> instance, which represents converted color value.</returns>
 /// 
 public static YCbCr FromRGB(RGB rgb)
 {
   YCbCr ycbcr = new YCbCr();
   FromRGB(rgb, ycbcr);
   return ycbcr;
 }
Esempio n. 7
0
    /// <summary>
    /// Convert from YCbCr to RGB color space.
    /// </summary>
    /// 
    /// <param name="ycbcr">Source color in <b>YCbCr</b> color space.</param>
    /// <param name="rgb">Destination color in <b>RGB</b> color spacs.</param>
    /// 
    public static void ToRGB(YCbCr ycbcr, RGB rgb)
    {
      // don't warry about zeros. compiler will remove them
      float r = Math.Max(0.0f, Math.Min(1.0f, (float)(ycbcr.Y + 0.0000 * ycbcr.Cb + 1.4022 * ycbcr.Cr)));
      float g = Math.Max(0.0f, Math.Min(1.0f, (float)(ycbcr.Y - 0.3456 * ycbcr.Cb - 0.7145 * ycbcr.Cr)));
      float b = Math.Max(0.0f, Math.Min(1.0f, (float)(ycbcr.Y + 1.7710 * ycbcr.Cb + 0.0000 * ycbcr.Cr)));

      rgb.Red = (byte)(r * 255);
      rgb.Green = (byte)(g * 255);
      rgb.Blue = (byte)(b * 255);
    }