Я написал преобразование HEX в RGB на C#, и ввод/вывод консоли терминала библиотеки Crayon не может понять значение метоC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Я написал преобразование HEX в RGB на C#, и ввод/вывод консоли терминала библиотеки Crayon не может понять значение мето

Сообщение Anonymous »


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?
But I was not looking for these answers.
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;
}
I assigned the method and its value to the new variable:

Код: Выделить всё

var goldenYellow = HexToRGBColour("#B38D1E");
Console.WriteLine($"The RGB of #B38D1E is {goldenYellow}");
It resulted as

Код: Выделить всё

179, 141, 30
and it was correct.
It worked fully.
But I assigned them to Crayon's library input/output and it failed.
The original code:

Код: Выделить всё

Console.WriteLine($"{Rgb(179, 141, 30).Text("Preview of the colour")}");
It worked fully.
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 first code failed with a following error:

Код: Выделить всё

The error CS7036: There is no argument given that corresponds to the required parameter 'g' of 'Output.Rgb(byte, byte, byte)'
The second code failed with the same error.
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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»