ColorsConverter.cs
Description du code
ColorsConverter.cs est un fichier du projet BrolDev.Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.
Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.
Code source ou contenu du fichier
Code c# (ColorsConverter.cs) (206 lignes)
using System; using be.gaudry.model.config; namespace be.gaudry.model.drawing.colors.converter { /// <summary> /// Color converter - converts colors from different color spaces /// </summary> /// /// <remarks>The class provides static method, which implement conversation /// between <b>RGB</b> and other color palettes.</remarks> /// public sealed class ColorsConverter { "AForge.NET", "ColorsConverter", "http://aforge.googlecode.com/svn/trunk/Sources/", "Framework de traitement d'images" ) ); // Avoid class instantiation private ColorsConverter() { } /// <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> public static void RGB2HSL(RGB rgb, HSL hsl) { double r = (rgb.Red / 255.0); double g = (rgb.Green / 255.0); double b = (rgb.Blue / 255.0); double min = Math.Min(Math.Min(r, g), b); double max = Math.Max(Math.Max(r, g), b); double delta = max - min; // get luminance value hsl.Luminance = (float)((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) ? (float)(delta / (max + min)) : (float)(delta / (2 - max - min)); // get hue value double del_r = (((max - r) / 6) + (delta / 2)) / delta; double del_g = (((max - g) / 6) + (delta / 2)) / delta; double del_b = (((max - b) / 6) + (delta / 2)) / delta; double hue; if (r == max) hue = del_b - del_g; else if (g == max) hue = (1.0 / 3) + del_r - del_b; else hue = (2.0 / 3) + del_g - del_r; // correct hue if needed if (hue < 0) hue += 1; if (hue > 1) hue -= 1; hsl.Hue = (int)(hue * 360); } } /// <summary> /// Convert from RGB to HSL color space /// </summary> /// <param name="rgb">Source color in <b>RGB</b> color space</param> public static HSL RGB2HSL(RGB rgb) { RGB2HSL(rgb, hsl); return hsl; } /// <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 HSL2RGB(HSL hsl, RGB rgb) { if (hsl.Saturation == 0) { // gray values rgb.Red = rgb.Green = rgb.Blue = (byte)(hsl.Luminance * 255); } else { double v1, v2; double hue = (double)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.0 / 3))); rgb.Green = (byte)(255 * Hue_2_RGB(v1, v2, hue)); rgb.Blue = (byte)(255 * Hue_2_RGB(v1, v2, hue - (1.0 / 3))); } } /// <summary> /// Convert from HSL to RGB color space /// </summary> /// <param name="hsl">Source color in <b>HSL</b> color space</param> /// public static RGB HSL2RGB(HSL hsl) { HSL2RGB(hsl, rgb); return rgb; } /// <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 RGB2YCbCr(RGB rgb, YCbCr ycbcr) { double r = (double)rgb.Red / 255; double g = (double)rgb.Green / 255; double b = (double)rgb.Blue / 255; ycbcr.Y = 0.2989 * r + 0.5866 * g + 0.1145 * b; ycbcr.Cb = -0.1687 * r - 0.3313 * g + 0.5000 * b; ycbcr.Cr = 0.5000 * r - 0.4184 * g - 0.0816 * b; } /// <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> /// public static YCbCr RGB2YCbCr(RGB rgb) { RGB2YCbCr(rgb, ycbcr); return ycbcr; } /// <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 YCbCr2RGB(YCbCr ycbcr, RGB rgb) { // don't warry about zeros. compiler will remove them double r = Math.Max(0.0, Math.Min(1.0, ycbcr.Y + 0.0000 * ycbcr.Cb + 1.4022 * ycbcr.Cr)); double g = Math.Max(0.0, Math.Min(1.0, ycbcr.Y - 0.3456 * ycbcr.Cb - 0.7145 * ycbcr.Cr)); double b = Math.Max(0.0, Math.Min(1.0, 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); } /// <summary> /// Convert from YCbCr to RGB color space /// </summary> /// /// <param name="ycbcr">Source color in <b>YCbCr</b> color space</param> /// public static RGB YCbCr2RGB(YCbCr ycbcr) { YCbCr2RGB(ycbcr, rgb); return rgb; } #region Private members // HSL to RGB helper routine private static double Hue_2_RGB(double v1, double v2, double vH) { if (vH < 0) vH += 1; if (vH > 1) vH -= 1; if ((6 * vH) < 1) return (v1 + (v2 - v1) * 6 * vH); if ((2 * vH) < 1) return v2; if ((3 * vH) < 2) return (v1 + (v2 - v1) * ((2.0 / 3) - vH) * 6); return v1; } #endregion } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732346175 23/11/2024 08:16:15 |
Utilisation de l'explorateur de code
- Navigation :
- Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
- Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
- La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
- Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
- Affichage :
- Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
- Actions :
- Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 16/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-broldev-source-rf-model/drawing/colors/converter/ColorsConverter.cs.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.