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)
- {
- if (red > 255)
- {
- red = 255;
- }
- if (red < 0)
- {
- red = 0;
- }
- if (green > 255)
- {
- green = 255;
- }
- if (green < 0)
- {
- green = 0;
- }
- if (blue > 255)
- {
- blue = 255;
- }
- if (blue < 0)
- {
- blue = 0;
- }
- static string DigitToHex(int color)
- {
- return color switch
- {
- 0 => "0",
- 1 => "1",
- 2 => "2",
- 3 => "3",
- 4 => "4",
- 5 => "5",
- 6 => "6",
- 7 => "7",
- 8 => "8",
- 9 => "9",
- 10 => "A",
- 11 => "B",
- 12 => "C",
- 13 => "D",
- 14 => "E",
- 15 => "F",
- _ => "-1"
- };
- }
- return DigitToHex((red / 16) % 16) + DigitToHex(red % 16) + DigitToHex((green / 16) % 16) + DigitToHex(green % 16) + DigitToHex((blue / 16) % 16) + DigitToHex(blue % 16);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement