Converting Color to ConsoleColor?

前端 未结 8 1340
面向向阳花
面向向阳花 2020-12-02 21:22

What is the best way to convert a System.Drawing.Color to a similar System.ConsoleColor?

8条回答
  •  离开以前
    2020-12-02 21:50

    Easy one...

    Public Shared Function ColorToConsoleColor(cColor As Color) As ConsoleColor
            Dim cc As ConsoleColor
            If Not System.Enum.TryParse(Of ConsoleColor)(cColor.Name, cc) Then
                Dim intensity = If(Color.Gray.GetBrightness() < cColor.GetBrightness(), 8, 0)
                Dim r = If(cColor.R >= &H80, 4, 0)
                Dim g = If(cColor.G >= &H80, 2, 0)
                Dim b = If(cColor.B >= &H80, 1, 0)
    
                cc = CType(intensity + r + g + b, ConsoleColor)
            End If
            Return cc
        End Function
    

提交回复
热议问题