2

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).

mate64
  • 9,876
  • 17
  • 64
  • 96
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 5
    It fails because some ConsoleColor names do not have a System.Drawing.Colors counterpart. "DarkYellow" for instance. Since ther are only 15 or 16 of them, I'd just map them like this http://stackoverflow.com/a/1988854/1070452 ; some of the other answers are interesting too – Ňɏssa Pøngjǣrdenlarp Jan 13 '14 at 13:39
  • 1
    @ElektroStudios you should think about accepting mafu's answer – fubo Feb 08 '16 at 15:26

3 Answers3

9

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

SSS
  • 4,807
  • 1
  • 23
  • 44
mafu
  • 31,798
  • 42
  • 154
  • 247
  • I know that for Console color "Green" (#00FF00) is called "Lime" in System.Drawing. As you show above System.Drawing.Green = ConsoleColor.DarkGreen. And there are about 50 shades of gray. (7 really). – Jroonk Oct 12 '19 at 01:59
2

how about,

Module ColorExtension

    <Extension()>
    Public Function DrawingColor(ByVal color As ConsoleColor) As Color
        Select color
            Case ConsoleColor.Black
                Return Color.Black

            Case ConsoleColor.Blue
                Return Color.Blue

            Case ConsoleColor.Cyan
                Return Color.Cyan

            Case ConsoleColor.DarkBlue
                Return ColorTranslator.FromHtml("#000080")

            Case ConsoleColor.DarkGray
                Return ColorTranslator.FromHtml("#808080")

            Case ConsoleColor.DarkGreen
                Return ColorTranslator.FromHtml("#008000")

            Case ConsoleColor.DarkMagenta
                Return ColorTranslator.FromHtml("#800080")

            Case ConsoleColor.DarkRed
                Return ColorTranslator.FromHtml("#800000")

            Case ConsoleColor.DarkYellow
                Return ColorTranslator.FromHtml("#808000")

            Case ConsoleColor.Gray
                Return ColorTranslator.FromHtml("#C0C0C0")

            Case ConsoleColor.Green
                Return ColorTranslator.FromHtml("#00FF00")

            Case ConsoleColor.Magenta
                Return Color.Magenta

            Case ConsoleColor.Red
                Return Color.Red

            Case ConsoleColor.White
                Return Color.White

            Case Else
                Return Color.Yellow
        End Select
    End Function
End Module

Allowing,

Dim result As Drawing.Color = ConsoleColor.Red.DrawingColor()
Jodrell
  • 34,946
  • 5
  • 87
  • 124
1

Just an adaptation of @Jodrell solution

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

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

        Select Case c.IsKnownColor

            ' Fix for the 'Consolecolor.DarkYellow' value which doesn't have color information.
            Case False
                Return Color.FromArgb(255, 128, 128, 0)

            Case Else
                Return c

        End Select

    End Function
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417