c# rgb to consolecolor
// Console.ForegroundColor = RGBToConsoleColor(c);
public ConsoleColor RGBToConsoleColor (Color color) {
if (color.GetSaturation () < 0.5)
switch ((int) (color.GetBrightness () * 3.5)) {
case 0:
return ConsoleColor.Black;
case 1:
return ConsoleColor.DarkGray;
case 2:
return ConsoleColor.Gray;
default:
return ConsoleColor.White;
}
var hue = (int) Math.Round (color.GetHue () / 60, MidpointRounding.AwayFromZero);
if (color.GetBrightness () < 0.4)
switch (hue) {
case 1:
return ConsoleColor.DarkYellow;
case 2:
return ConsoleColor.DarkGreen;
case 3:
return ConsoleColor.DarkCyan;
case 4:
return ConsoleColor.DarkBlue;
case 5:
return ConsoleColor.DarkMagenta;
default:
return ConsoleColor.DarkRed;
}
switch (hue) {
case 1:
return ConsoleColor.Yellow;
case 2:
return ConsoleColor.Green;
case 3:
return ConsoleColor.Cyan;
case 4:
return ConsoleColor.Blue;
case 5:
return ConsoleColor.Magenta;
default:
return ConsoleColor.Red;
}
}
4.2
5
int index = (c.R > 128 | c.G > 128 | c.B > 128) ? 8 : 0;
index |= (c.R > 64) ? 4 : 0;
index |= (c.G > 64) ? 2 : 0;
index |= (c.B > 64) ? 1 : 0;
Console.ForegroundColor = (System.ConsoleColor)index;
Thank you!
5
0
Are there any code examples left?
New code examples in category C#
-
C# 2022-03-27 22:40:23 c# movement script
-
C# 2022-03-27 22:15:17 Delete last modification on EntityFramework Core
-
C# 2022-03-27 22:15:02 file.create folder c#
-
C# 2022-03-27 19:25:04 Error inflating class android.support.constraint.ConstraintLayout
-
C# 2022-03-27 19:15:02 top down shooting in unity 2D
-
C# 2022-03-27 18:50:02 c# remove invalid directory characters
-
C# 2022-03-27 18:30:07 how to use K2 games Games parallax background
-
C# 2022-03-27 18:25:10 c# replace crlf
-
C# 2022-03-27 18:25:09 keycode for left mouse click unity