27. september 2006 - 10:49Der er
7 kommentarer og 1 løsning
HSB farveskala.
Mig: .net novice Mit mål: et program til at lave RGB-HEX koder. Mit situation Jeg har brug for et system / kode til at beregne R G B værdier i respektive textbokse for henholdsvis HUE Brightness og saturation.
Jeg har oversat den Structure, som er at finde på sidstnævnte link:
Public Structure tHSBColor Private mH As Single Private mS As Single Private mB As Single Private mA As Integer
Public Sub New(ByVal pHue As Single, _ ByVal pSaturation As Single, _ ByVal pBrightness As Single) Me.mA = 255 Me.mH = Math.Min(Math.Max(pHue, 0), 255) Me.mS = Math.Min(Math.Max(pSaturation, 0), 255) Me.mB = Math.Min(Math.Max(pBrightness, 0), 255) End Sub
Public Sub New(ByVal pAlpha As Integer, _ ByVal pHue As Single, _ ByVal pSaturation As Single, _ ByVal pBrightness As Single) Me.mA = pAlpha Me.mH = Math.Min(Math.Max(pHue, 0), 255) Me.mS = Math.Min(Math.Max(pSaturation, 0), 255) Me.mB = Math.Min(Math.Max(pBrightness, 0), 255) End Sub
Public Sub New(ByVal pColor As Color) Dim lColor As tHSBColor = FromColor(Color) Me.mA = lColor.mA Me.mH = lColor.mH Me.mS = lColor.mS Me.mB = lColor.mB End Sub
Public ReadOnly Property H() As Single Get Return mH End Get End Property
Public ReadOnly Property S() As Single Get Return mS End Get End Property
Public ReadOnly Property B() As Single Get Return mB End Get End Property
Public ReadOnly Property A() As Integer Get Return mA End Get End Property
Public ReadOnly Property Color() As Color Get Return FromHSB(Me) End Get End Property
Public Shared Function ShiftHue(ByVal pColor As Color, _ ByVal pHueDelta As Single) As Color Dim hsb As tHSBColor = tHSBColor.FromColor(pColor) hsb.mH += pHueDelta hsb.mH = Math.Min(Math.Max(hsb.mH, 0), 255) Return FromHSB(hsb) End Function
Public Shared Function ShiftSaturation(ByVal pColor As Color, _ ByVal pSaturationDelta As Single) As Color Dim hsb As tHSBColor = tHSBColor.FromColor(pColor) hsb.mS += pSaturationDelta hsb.mS = Math.Min(Math.Max(hsb.mS, 0), 255) Return FromHSB(hsb) End Function
Public Shared Function ShiftBrightness(ByVal pColor As Color, _ ByVal pBrightnessDelta As Single) As Color Dim hsb As tHSBColor = tHSBColor.FromColor(pColor) hsb.mB += pBrightnessDelta hsb.mB = Math.Min(Math.Max(hsb.mB, 0), 255) Return fromHSB(hsb) End Function
Public Shared Function FromHSB(ByVal pHSBColor As tHSBColor) As Color Dim lR As Single = pHSBColor.mB Dim lG As Single = pHSBColor.mB Dim lB As Single = pHSBColor.mB If Not (pHSBColor.mS = 0) Then Dim lMax As Single = pHSBColor.mB Dim lDif As Single = pHSBColor.mB * pHSBColor.mS / 255.0F Dim lMin As Single = pHSBColor.mB - lDif Dim lH As Single = pHSBColor.mH * 360.0F / 255.0F If lH < 60.0F Then lR = lMax lG = lH * lDif / 60.0F + lMin lB = lMin Else If lH < 120.0F Then lR = -(lH - 120.0F) * lDif / 60.0F + lMin lG = lMax lB = lMin Else If lH < 180.0F Then lR = lMin lG = lMax lB = (lH - 120.0F) * lDif / 60.0F + lMin Else If lH < 240.0F Then lR = lMin lG = -(lH - 240.0F) * lDif / 60.0F + lMin lB = lMax Else If lH < 300.0F Then lR = (lH - 240.0F) * lDif / 60.0F + lMin lG = lMin lB = lMax Else If lH <= 360.0F Then lR = lMax lG = lMin lB = -(lH - 360.0F) * lDif / 60 + lMin Else lR = 0 lG = 0 lB = 0 End If End If End If End If End If End If End If Return Color.FromArgb(pHSBColor.mA, _ CType(Math.Round(Math.Min(Math.Max(lR, 0), 255)), Integer), _ CType(Math.Round(Math.Min(Math.Max(lG, 0), 255)), Integer), _ CType(Math.Round(Math.Min(Math.Max(lB, 0), 255)), Integer)) End Function
Public Shared Function FromColor(ByVal pColor As Color) As tHSBColor Dim lReturn As tHSBColor = New tHSBColor(0.0F, 0.0F, 0.0F) Dim lR As Single = pColor.R Dim lG As Single = pColor.G Dim lB As Single = pColor.B lReturn.mA = pColor.A Dim max As Single = Math.Max(lR, Math.Max(lG, lB)) If max <= 0 Then Return lReturn End If Dim min As Single = Math.Min(lR, Math.Min(lG, lB)) Dim dif As Single = max - min If max > min Then If lG = max Then lReturn.mH = (lB - lR) / dif * 60.0F + 120.0F Else If lB = max Then lReturn.mH = (lR - lG) / dif * 60.0F + 240.0F Else If lB > lG Then lReturn.mH = (lG - lB) / dif * 60.0F + 360.0F Else lReturn.mH = (lG - lB) / dif * 60.0F End If End If End If If lReturn.mH < 0 Then lReturn.mH = lReturn.mH + 360.0F End If Else lReturn.mH = 0 End If lReturn.mH *= 255.0F / 360.0F lReturn.mS = (dif / max) * 255.0F lReturn.mB = max Return lReturn End Function End Structure
Hvis du har prøvet at arbejde med System.Drawing.Color klassen i .NET, så skulle du også kunne bruge tHSBColor klassen.
For at oprette en farve ud fra hue, saturation og brightness:
Dim lMyColor As New tHSBColor(100, 100, 100) Me.BackColor = lMyColor.Color
For at få fat på HSB værdier baseret på en alm. RGB farve:
Dim lMyColor As New tHSBColor(Color.Green) txtHue.Text = lMyColor.H.ToString() txtSaturation.Text = lMyColor.S.ToString() txtBrightness.Text = lMyColor.B.ToString()
For at få fat på RGB værdier baseret på en HSB farve:
Dim lMyColor As New tHSBColor(100, 100, 100) Dim lColor As Color = lMyColor.Color txtRed.Text = lColor.R.ToString() txtGreen.Text = lColor.G.ToString() txtBlue.Text = lColor.B.ToString()
tHSBColor er navnet på den klasse, som jeg skrev koden til i mit andet indlæg.
Opret en ny fil med navnet "HSBColor.vb" (hvis du arbejder med Visual Studio, skal filen være af typen Class). Fjern alt, hvad der i forvejen måtte stå i filen, og copy/paste indholdet af mit indlæg nr. 2 (27/09-2006 23:08:02) ind i filen (Alt fra "Public Structure ..." til "End Structure"). Gem filen.
Nu har du tHSBColor klassen, som du kan arbejde med, stortset ligesom man arbejder med System.Drawing.Color klassen, som findes i .NET fra start af.
Du kan nu bruge kodeeksemplerne, som jeg skrev i indlæg nr 3 (27/09-2006 23:16:51) til at arbejde med tHSBColor klassen og manipulere med farver ud fra en HSB skala.
Faktisk udtaler jeg mig forkert. Der er egentlig ikke tale om en klasse, men en structure, hvilket er to forskellige ting. Men man arbejder med dem på stortset samme facon.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.