Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Consolecolor to Color?

How I could convert a ConsoleColor to a Color type?

I need this for create an overload of this method, which should return a color instead a Consolecolor:

    ''' <summary>
    ''' Generates a random ConsoleColor color.
    ''' </summary>
    ''' <returns>ConsoleColor.</returns>
    Public Shared Function [ConsoleColor]() As ConsoleColor

        Dim Rand As New Random
        Return [Enum].Parse(GetType(ConsoleColor), 
                            Rand.Next(0, 15))

    End Function

This is what I've tried, but sometimes the returned color is empty because the ConsoleColor name is unknown:

    ''' <summary>
    ''' Generates a random QB color.
    ''' </summary>
    ''' <returns>Color.</returns>
    Public Shared Function QB() As Color

        Dim Rand As New Random
        Return Color.FromName([Enum].Parse(GetType(ConsoleColor), 
                              Rand.Next(0, 15)).ToString)

    End Function

PS: I want to avoid the usage of old VB6 methods (QBColor function).

like image 375
ElektroStudios Avatar asked Jan 22 '26 09:01

ElektroStudios


1 Answers

I've had this problem myself today and cannot agree with both current answers.

I ended up checking all values and found that the values are very different, I have no idea why people recommend converting 'by name'.

## Name         Actual      Drawing.Color of same name
 0 Black        #000000     #000000
 1 DarkBlue     #000080     #00008B
 2 DarkGreen    #008000     #006400
 3 DarkCyan     #008080     #008B8B
 4 DarkRed      #800000     #8B0000
 5 DarkMagenta  #800080     #8B008B
 6 DarkYellow   #808000     #000000
 7 Gray         #C0C0C0     #808080
 8 DarkGray     #808080     #A9A9A9
 9 Blue         #0000FF     #0000FF
10 Green        #00FF00     #008000
11 Cyan         #00FFFF     #00FFFF
12 Red          #FF0000     #FF0000
13 Magenta      #FF00FF     #FF00FF
14 Yellow       #FFFF00     #FFFF00
15 White        #FFFFFF     #FFFFFF

CC RGB test

like image 136
mafu Avatar answered Jan 24 '26 18:01

mafu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!