I have seen the question How to convert Hex to RGB?, but their answers were not what I looked for. I also have seen the following questions:
- Convert ARGB hex string to RGB
- How do I get the color from a hexadecimal color code using .NET?
I use C#/Nuget library Crayon that allows me to define a colour for the console output using ANSI escape codes.
I wrote a small code in C# to convert the hexadecimal code colour to the RGB colour and I assigned the method value to Crayon's libray input/output.
Код: Выделить всё
static string HexToRGBColour(string hex)
{
int r = Convert.ToInt32(hex.Substring(1, 2), 16);
int g = Convert.ToInt32(hex.Substring(3, 2), 16);
int b = Convert.ToInt32(hex.Substring(5, 2), 16);
return r + ", " + g + ", " + b;
}
Код: Выделить всё
var goldenYellow = HexToRGBColour("#B38D1E");
Console.WriteLine($"The RGB of #B38D1E is {goldenYellow}");
Код: Выделить всё
179, 141, 30
It worked fully.
But I assigned them to Crayon's library
Код: Выделить всё
Rgb()
The original code:
Код: Выделить всё
Console.WriteLine($"{Rgb(179, 141, 30).Text("Preview of the colour")}");
I wanted to get rid of the RGB, and use to the hexadecimal code instead. Here is the modified code with two alternatives:
Код: Выделить всё
Console.WriteLine($"{Rgb(goldenYellow).Text("Preview of the colour")}");
Console.WriteLine($"{Rgb(HexToRGBColour("#B38D1E")).Text("Preview of the colour")}");
Код: Выделить всё
The error CS7036: There is no argument given that corresponds to the required parameter 'g' of 'Output.Rgb(byte, byte, byte)'
I expected that both these codes would become
Код: Выделить всё
Console.WriteLine($"{Rgb(179, 141, 30).Text("Preview of the colour")}")
Remember I use .NET Core 8 and Linux and that I want to use an OS-agnostic code.
Источник: https://stackoverflow.com/questions/781 ... onsole-inp