2014-01-13 123 views
2

如何將ConsoleColor轉換爲Color類型?將Consolecolor轉換爲彩色?

我需要這個創建這個方法的重載,它應該返回一個顏色,而不是一個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 

這是我嘗試過,但有時返回的顏色是空的,因爲ConsoleColor名未知:

''' <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:我想避免的舊VB6的方法(QBColor功能)的使用。

+5

因爲一些ConsoleColor名沒有System.Drawing.Colors對口它失敗。例如「DarkYellow」。由於它們只有15或16個,所以我只是將它們映射爲http://stackoverflow.com/a/1988854/1070452;一些其他答案也很有趣 – Plutonix

+1

@ElektroStudios你應該考慮接受馬伕的答案 – fubo

回答

5

我有這個問題,我今天不能達成一致與當前的答案。

我結束了檢查所有的值,發現值是非常不同的,我不知道爲什麼人們建議轉換'按名稱'。

Name   Actual  Drawing.Color of same name 
Black  #000000  #000000 
DarkBlue  #000080  #00008B 
DarkGreen #008000  #006400 
DarkCyan  #008080  #008B8B 
DarkRed  #800000  #8B0000 
DarkMagenta #800080  #8B008B 
DarkYellow #808000  #000000 
Gray   #C0C0C0  #808080 
DarkGray  #808080  #A9A9A9 
Blue   #0000FF  #0000FF 
Green  #00FF00  #008000 
Cyan   #00FFFF  #00FFFF 
Red   #FF0000  #FF0000 
Magenta  #FF00FF  #FF00FF 
Yellow  #FFFF00  #FFFF00 
White  #FFFFFF  #FFFFFF 

CC RGB test

+2

這應該被標記爲正確的答案 – fubo

2

怎麼樣,

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 

允許,

Dim result As Drawing.Color = ConsoleColor.Red.DrawingColor() 
+1

這是要做到這一點的唯一方法。 ConsoleColor不包含任何實際的顏色信息。 – Ashigore

+0

謝謝,我可以適應我的功能 – ElektroStudios

+0

@Jodrell DarkYellow的顏色怎麼樣?這是完全相同的? (在枚舉中找不到赭石) – ElektroStudios

1

只是@Jodrell解決方案適應

''' <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