Advertisement
yahorrr

Untitled

Jun 3rd, 2022
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. using System;
  2.  
  3. namespace RgbConverter
  4. {
  5.     public static class Rgb
  6.     {
  7.         /// <summary>
  8.         /// Gets hexadecimal representation source RGB decimal values.
  9.         /// </summary>
  10.         /// <param name="red">The valid decimal value for RGB is in the range 0-255.</param>
  11.         /// <param name="green">The valid decimal valu for RGB is in the range 0-255.</param>
  12.         /// <param name="blue">The valid decimal val for RGB is in the range 0-255.</param>
  13.         /// <returns>Returns hexadecimal representation source RGB decimal values.</returns>
  14.         public static string GetHexRepresentation(int red, int green, int blue)
  15.         {
  16.             if (red > 255)
  17.             {
  18.                 red = 255;
  19.             }
  20.  
  21.             if (red < 0)
  22.             {
  23.                 red = 0;
  24.             }
  25.  
  26.             if (green > 255)
  27.             {
  28.                 green = 255;
  29.             }
  30.  
  31.             if (green < 0)
  32.             {
  33.                 green = 0;
  34.             }
  35.  
  36.             if (blue > 255)
  37.             {
  38.                 blue = 255;
  39.             }
  40.  
  41.             if (blue < 0)
  42.             {
  43.                 blue = 0;
  44.             }
  45.  
  46.             static string DigitToHex(int color)
  47.             {
  48.                 return color switch
  49.                 {
  50.                     0 => "0",
  51.                     1 => "1",
  52.                     2 => "2",
  53.                     3 => "3",
  54.                     4 => "4",
  55.                     5 => "5",
  56.                     6 => "6",
  57.                     7 => "7",
  58.                     8 => "8",
  59.                     9 => "9",
  60.                     10 => "A",
  61.                     11 => "B",
  62.                     12 => "C",
  63.                     13 => "D",
  64.                     14 => "E",
  65.                     15 => "F",
  66.                     _ => "-1"
  67.                 };
  68.             }
  69.  
  70.             return DigitToHex((red / 16) % 16) + DigitToHex(red % 16) + DigitToHex((green / 16) % 16) + DigitToHex(green % 16) + DigitToHex((blue / 16) % 16) + DigitToHex(blue % 16);
  71.         }
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement