Problem bei Color zu String

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von ~blaze~.

    Problem bei Color zu String

    Hi
    Ich habe einen Fehler den ich irgendwie nicht ganz versteh: Ich hab bei meiner IniLib ja als Angebot eine Methode namens ColorToString reingebaut. Das funktioniert aber nicht ganz(wie so manches ;)). Die Funktion sieht so aus:

    VB.NET-Quellcode

    1. Public Shared Function ColorToString(ByVal Color As Color, Optional ByVal Pattern As String = "[R],[G],[B]") As String
    2. Dim Retval As String = Pattern
    3. Retval = Regex.Replace(Retval, "\[ARGB\]", Color.ToArgb.ToString, RegexOptions.IgnoreCase)
    4. Retval = Regex.Replace(Retval, "\[BR\]", Color.GetBrightness.ToString, RegexOptions.IgnoreCase)
    5. Retval = Regex.Replace(Retval, "\[A\]", Color.A.ToString, RegexOptions.IgnoreCase)
    6. Retval = Regex.Replace(Retval, "\[R\]", Color.R.ToString, RegexOptions.IgnoreCase)
    7. Retval = Regex.Replace(Retval, "\[G\]", Color.G.ToString, RegexOptions.IgnoreCase)
    8. Retval = Regex.Replace(Retval, "\[B\]", Color.B.ToString, RegexOptions.IgnoreCase)
    9. Retval = Regex.Replace(Retval, "\[H\]", Color.GetHue.ToString, RegexOptions.IgnoreCase)
    10. Retval = Regex.Replace(Retval, "\[S\]", Color.GetSaturation.ToString, RegexOptions.IgnoreCase)
    11. Return Retval
    12. End Function

    jetzt ist da allerdings der Fehler, dass das Ergebnis falsch ist. Was mache ich da falsch? ^^

    VB.NET-Quellcode

    1. Public Class ColorConvertion
    2. Public Shared Function ColorToString(ByVal Color As Color) As String
    3. Return Functions.Converter.ColorToString(Color)
    4. End Function
    5. '...
    6. End Class


    Der Fehler scheint beim ColorToString-Aufruf zu liegen, da für die Farbe Color.FromArgb(0, 0, 128) (Dunkelblau) 128,0,128(Violett) herauskommt. Es sieht so aus, dass da R falsch eingesetzt wird. Aber da fällt mir bei meinem Code nichts auf...


    Gruß
    ~blaze~
    Ja. Beim ersten Durchlauf ists noch dunkelblau, beim 2. lila..., was ich seltsam finde, weil dann der Fehler in der IniLib liegen müsste, was allerdings ja nicht möglich ist, da die ja den String nicht noch mal umstellt...

    Edit: Mir ist jetz noch was aufgefallen... Ich hab immer die Anwendung 2x gestartet(-->Überschreiben der Einstellungen) und dann geschaut wies aussieht... Das liegt daran, dass das Einlesen nicht klappt... Muss mich mal noch mal in den Code einlesen...

    Edit2: ^^ Finde da auch nix...

    VB.NET-Quellcode

    1. Public Shared Sub StringToObject(ByVal Value As String, ByRef obj As Object, ByVal TypeHandlers() As TypeHandler, ByVal ParamArray Properties() As String)
    2. Dim strarr() As String = IniOperations.GetAllValues(Value)
    3. Dim tp As New List(Of String)
    4. For Each x As Type In TypeHandlers
    5. If tp.Contains(x.FullName) Then
    6. Throw New Exception("There are multiple definitions for TypeHandler type '" & x.FullName & "'.")
    7. End If
    8. tp.Add(x.FullName)
    9. Next
    10. For i As Integer = 0 To Properties.Length - 1
    11. Dim p As PropertyInfo = obj.GetType.GetProperty(Properties(i))
    12. If tp.Contains(p.PropertyType.FullName) Then
    13. p.SetValue(obj, TypeHandlers(tp.IndexOf(p.PropertyType.FullName)).Handler.Invoke(p.PropertyType, strarr(i)), BindingFlags.Default, Nothing, Nothing, Nothing)
    14. ElseIf tp.Contains(GetType(Object).FullName) Then
    15. p.SetValue(obj, TypeHandlers(tp.IndexOf(GetType(Object).FullName)).Handler.Invoke(p.PropertyType, strarr(i)), BindingFlags.Default, Nothing, Nothing, Nothing)
    16. Else
    17. Throw New Exception("TypeHandlers() must contain a TypeHandler for type System.Object or " & p.PropertyType.FullName & ".")
    18. End If
    19. Next
    20. End Sub

    Value ist der Wert in der INI-Datei, obj ist das Objekt, dessen Eigenschaft gesetzt werden soll, TypeHandler sind einfach nur Delegaten, die Typen zugeordnet werden und Properties sind die Eigenschaften, die gesetzt werden sollen.

    Der Aufruf im Programm sieht so aus:

    VB.NET-Quellcode

    1. Public Shared Function StringToColor(ByVal str As String) As Color
    2. Dim conv As New Functions.TypeHandler(GetType(Byte), AddressOf GetByteFromString)
    3. Dim changeableColor As New ChangeableColor
    4. Functions.Converter.StringToObject(str, changeableColor, New Functions.TypeHandler() {conv}, "R", "G", "B")
    5. Return Color.FromArgb(changeableColor.R, changeableColor.G, changeableColor.B)
    6. End Function
    7. Private Shared Function GetByteFromString(ByVal tp As Type, ByVal bt As String) As Byte
    8. Return CByte(bt)
    9. End Function
    10. Private Class ChangeableColor
    11. Private btR As Byte
    12. Private btG As Byte
    13. Private btB As Byte
    14. Public Property R() As Byte
    15. Get
    16. Return btR
    17. End Get
    18. Set(ByVal value As Byte)
    19. btR = value
    20. End Set
    21. End Property
    22. Public Property G() As Byte
    23. Get
    24. Return btG
    25. End Get
    26. Set(ByVal value As Byte)
    27. btG = value
    28. End Set
    29. End Property
    30. Public Property B() As Byte
    31. Get
    32. Return btR
    33. End Get
    34. Set(ByVal value As Byte)
    35. btR = value
    36. End Set
    37. End Property
    38. End Class


    ChangeableColor wird verwendet, da R, G und B von Color ReadOnly sind und deshalb nicht gesetzt werden können. Die Frage ist halt jetzt warum das nicht funktioniert ^^

    Edit: Hab den Fehler :/... War wohl n bissl spät:
    Bei der Property B wird der Wert btR zurück gegeben und gesetzt, was natürlich Schwachsinn ist...

    Gruß
    ~blaze~

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „~blaze~“ ()