Hallo!
Probier mal sowas.
Das Testen der Klasse habe ich Dir überlassen.
Public Class RandomColors
Dim _coltab As New DataTable
Dim _mindiff As Integer
Dim _rndm As New Random
''' <summary>Erstellung einer Liste mit Zufallsfarben </summary>
''' <param name="Count">Anzahl der Farben (1-300)</param>
''' <param name="MinDiff">Mindestdifferenz, RGB gewichtet (0-30)</param>
Public Sub New(ByVal Count As Integer, ByVal MinDiff As Integer)
If Count < 1 Then Throw New Exception("Keine Farbe angefordert")
If Count > 300 Then Throw New Exception("Zu viele Farben")
If MinDiff < 0 Or MinDiff > 30 Then Throw New Exception("Ungültige" & _
"Schranke")
_coltab.TableName = "RandomColors"
_coltab.Columns.Add("Red", GetType(Byte))
_coltab.Columns.Add("Green", GetType(Byte))
_coltab.Columns.Add("Blue", GetType(Byte))
_mindiff = MinDiff
Dim counter As Integer, newcolor As Color
For i As Integer = 0 To Count - 1
_coltab.Rows.Add({0, 0, 0}) : counter = 0
Do
newcolor = RandomColor()
counter += 1
If counter > 500 Then
Throw New Exception("Ungeeignete Parameter")
End If
Loop While Not CheckColor(newcolor)
_coltab.Rows(i)("Red") = newcolor.R
_coltab.Rows(i)("Green") = newcolor.G
_coltab.Rows(i)("Blue") = newcolor.B
Next i
End Sub
''' <summary>Abfrage einer Farbe </summary>
''' <param name="index">Index der Liste</param>
Public ReadOnly Property GetColor(ByVal index As Integer) As Color
Get
Return Color.FromArgb(CInt(_coltab(index)("Red")), _
CInt(_coltab(index)("Green")), _
CInt(_coltab(index)("Blue")))
End Get
End Property
''' <summary> Anzahl der Farben </summary>
Public ReadOnly Property Count() As Integer
Get
Return _coltab.Rows.Count
End Get
End Property
''' <summary>Farben speichern</summary>
Public Function Save(ByVal Filename As String) As Boolean
Try
_coltab.WriteXml(Filename, XmlWriteMode.WriteSchema)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>Farben laden</summary>
Public Function Load(ByVal Filename As String) As Boolean
Try
_coltab.Rows.Clear()
Dim rm As XmlReadMode = _coltab.ReadXml(Filename)
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Function RandomColor() As Color
Return Color.FromArgb(_rndm.Next(0, 256),
_rndm.Next(0, 256), _rndm.Next(0, 256))
End Function
Private Function CheckColor(ByVal testcolor As Color) As Boolean
For i As Integer = 0 To _coltab.Rows.Count - 1
If ColorDiff(GetColor(i), testcolor) < _mindiff Then Return False
Next i
Return True
End Function
Private Function ColorDiff(ByVal a As Color, ByVal b As Color) As Integer
Return CInt(Math.Abs(CInt(a.R) - b.R) * 0.5) + _
CInt(Math.Abs(CInt(a.G) - b.G) * 0.4 + _
CInt(Math.Abs(CInt(a.B) - b.B) * 0.1))
End Function
End Class MfG
Manfred
Beitrag wurde zuletzt am 09.07.12 um 15:08:29 editiert. |