Advertisement
yahorrr

Untitled

Jun 3rd, 2022
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 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.             static string IntToHex(int color)
  17.             {
  18.                 if (color >= 255)
  19.                 {
  20.                     return "FF";
  21.                 }
  22.  
  23.                 if (color < 16)
  24.                 {
  25.                     switch (color)
  26.                     {
  27.                         case 0:
  28.                             return "00";
  29.                         case 1:
  30.                             return "01";
  31.                         case 2:
  32.                             return "02";
  33.                         case 3:
  34.                             return "03";
  35.                         case 4:
  36.                             return "04";
  37.                         case 5:
  38.                             return "05";
  39.                         case 6:
  40.                             return "06";
  41.                         case 7:
  42.                             return "07";
  43.                         case 8:
  44.                             return "08";
  45.                         case 9:
  46.                             return "09";
  47.                         case 10:
  48.                             return "0A";
  49.                         case 11:
  50.                             return "0B";
  51.                         case 12:
  52.                             return "0C";
  53.                         case 13:
  54.                             return "0D";
  55.                         case 14:
  56.                             return "0E";
  57.                         case 15:
  58.                             return "0F";
  59.                     }
  60.                 }
  61.  
  62.                 string hex = string.Empty;
  63.  
  64.                 for (int i = 0; i < 2; i++)
  65.                 {
  66.                     switch (color % 16)
  67.                     {
  68.                         case 0:
  69.                             hex += "0";
  70.                             break;
  71.                         case 1:
  72.                             hex += "1";
  73.                             break;
  74.                         case 2:
  75.                             hex += "2";
  76.                             break;
  77.                         case 3:
  78.                             hex += "3";
  79.                             break;
  80.                         case 4:
  81.                             hex += "4";
  82.                             break;
  83.                         case 5:
  84.                             hex += "5";
  85.                             break;
  86.                         case 6:
  87.                             hex += "6";
  88.                             break;
  89.                         case 7:
  90.                             hex += "7";
  91.                             break;
  92.                         case 8:
  93.                             hex += "8";
  94.                             break;
  95.                         case 9:
  96.                             hex += "9";
  97.                             break;
  98.                         case 10:
  99.                             hex += "A";
  100.                             break;
  101.                         case 11:
  102.                             hex += "B";
  103.                             break;
  104.                         case 12:
  105.                             hex += "C";
  106.                             break;
  107.                         case 13:
  108.                             hex += "D";
  109.                             break;
  110.                         case 14:
  111.                             hex += "E";
  112.                             break;
  113.                         case 15:
  114.                             hex += "F";
  115.                             break;
  116.                     }
  117.  
  118.                     color /= 16;
  119.                 }
  120.  
  121.                 return hex;
  122.             }
  123.  
  124.             return IntToHex(red) + IntToHex(green) + IntToHex(blue);
  125.         }
  126.     }
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement