2014-11-05 3 views
1
내가 객체의 색상 값을 기준으로 문자열을 그리려는

, 나는 단지의 SpriteBatch 정상적으로 그릴 간단한 것이라고 생각 :XNA에서 색상의 문자열 이름을 그리는 방법은 무엇입니까?

DrawString(Font, Color.ToString, Vector2, Colour) 

그러나 "Color.ToString"는 RGBA를 반환 (X, Y , z, a) 값을 포함 할 수있다. 사례를 처리하지 않고 RGBA 색상 (예 : "빨간색")의 속성 이름을 화면에 그려야하며 RGBA 값을 통해 색상을 결정하지 않아도됩니다. 시간과 코딩 공간을 절약 할 수 있습니다.

+0

질문을 풀지는 못하지만,'Color.ToString()'을 쓰는 것을 의미합니다. – gunr2171

+0

글쎄 VB.Net에 글을 쓰고있다. 언어에는 필요 없다. – JimmyB

답변

0

선택 항목을 명명 된 색상 값으로 제한하려면 System.Drawing.KnownColor을 대신 사용할 수 있습니다. 어쨌든 이름이있는 색상의 색상 이름을 원하면 Color.ToString() 대신 Color.Name을 사용할 수 있지만 색상이 RGBA 값이 아닌 알려진 색상으로 구성된 경우에만 작동합니다. 당신이 알려진 색상의 이름을 조회해야하는 경우,이 같은 함수를 작성하고 반환 된 색상의 이름을 얻을 수 :

Public Function FindKnownColor(value As Color) As Color 
    For Each known In [Enum].GetValues(GetType(KnownColor)) 
     Dim compare = Color.FromKnownColor(known) 
     If compare.ToArgb() = value.ToArgb() Then Return compare 
    Next 
    Return value 
End Function 

당신은 키가 어디 있는지 사전을 만들 수 있음을 최적화해야하는 경우 ToArgb 값이며이 값은이 함수가 반환하는 색상 객체이거나 단순히 해당 색상의 name 속성입니다.

0

XNA에서 Color 클래스는 열거 형이 아니므로 리플렉션을 사용하여 모든 정적 속성 값과 해당 이름을 가져와야합니다. 여기에 제공된 예제에서는 Color 값을 해당 이름에 매핑하는 정적 사전을 만듭니다. 사전은 class/ToName 함수를 처음 사용할 때 초기화됩니다.

' Usage: 
' Dim colorName = ColorExtensions.ToName(Color.Red) 

Public NotInheritable Class ColorExtensions 
    Private Sub New() 
    End Sub 
    Private Shared ReadOnly ColorToString As New Dictionary(Of Color, [String])() 

    Shared Sub New() 
     ' Get all the static properties on the XNA Color type 
     Dim properties = GetType(Color).GetProperties(BindingFlags.[Public] Or BindingFlags.[Static]) 

     ' Loop through all of the properties 
     For Each [property] As PropertyInfo In properties 
      ' If the property's type is a Color... 
      If [property].PropertyType Is GetType(Color) Then 
       ' Get the actual color value 
       Dim color = DirectCast([property].GetValue(Nothing, Nothing), Color) 

       ' We have to actually check that the color has not already been assocaited 
       ' Names will always be unique, however, some names map to the same color 
       If ColorToString.ContainsKey(color) = False Then 
        ' Associate the color value with the property name 
        ColorToString.Add(color, [property].Name) 
       End If 
      End If 
     Next 
    End Sub 

    Public Shared Function ToName(color As Color) As [String] 
     ' The string that stores the color's name 
     Dim name As [String] = Nothing 

     ' Attempt to get the color name from the dictionary 
     If ColorToString.TryGetValue(color, name) Then 
      Return name 
     End If 

     ' Return null since we didn't find it 
     Return Nothing 
    End Function 
End Class 
+0

VB.NET에서 기본적으로 작성하지 않기 때문에 C#에서 VB.NET으로 변환기를 실행 한 다음 VB XNA 게임으로 테스트하여 작동하는지 확인했습니다. – Trevor

관련 문제