Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace RgbConverter
- {
- public static class Rgb
- {
- /// <summary>
- /// Gets hexadecimal representation source RGB decimal values.
- /// </summary>
- /// <param name="red">The valid decimal value for RGB is in the range 0-255.</param>
- /// <param name="green">The valid decimal valu for RGB is in the range 0-255.</param>
- /// <param name="blue">The valid decimal val for RGB is in the range 0-255.</param>
- /// <returns>Returns hexadecimal representation source RGB decimal values.</returns>
- public static string GetHexRepresentation(int red, int green, int blue)
- {
- static string IntToHex(int color)
- {
- if (color >= 255)
- {
- return "FF";
- }
- if (color < 16)
- {
- switch (color)
- {
- case 0:
- return "00";
- case 1:
- return "01";
- case 2:
- return "02";
- case 3:
- return "03";
- case 4:
- return "04";
- case 5:
- return "05";
- case 6:
- return "06";
- case 7:
- return "07";
- case 8:
- return "08";
- case 9:
- return "09";
- case 10:
- return "0A";
- case 11:
- return "0B";
- case 12:
- return "0C";
- case 13:
- return "0D";
- case 14:
- return "0E";
- case 15:
- return "0F";
- }
- }
- string hex = string.Empty;
- for (int i = 0; i < 2; i++)
- {
- switch (color % 16)
- {
- case 0:
- hex += "0";
- break;
- case 1:
- hex += "1";
- break;
- case 2:
- hex += "2";
- break;
- case 3:
- hex += "3";
- break;
- case 4:
- hex += "4";
- break;
- case 5:
- hex += "5";
- break;
- case 6:
- hex += "6";
- break;
- case 7:
- hex += "7";
- break;
- case 8:
- hex += "8";
- break;
- case 9:
- hex += "9";
- break;
- case 10:
- hex += "A";
- break;
- case 11:
- hex += "B";
- break;
- case 12:
- hex += "C";
- break;
- case 13:
- hex += "D";
- break;
- case 14:
- hex += "E";
- break;
- case 15:
- hex += "F";
- break;
- }
- color /= 16;
- }
- return hex;
- }
- return IntToHex(red) + IntToHex(green) + IntToHex(blue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement