Casino online









Mercato forex







Appunti



Il concetto
Prima di iniziare a disegnare, bisogna capire come simulare il famoso effetto vetro. Dopo aver analizzato alcuni esempi, sono giunto a una conclusione alquanto semplice, ma di grande impatto. Il procedimento fondamentale che si dovrebbe utilizzare si può riassumere in questi punti: Ed ora passiamo al codice. Prima, prendiamo in prestito la classe HslColor:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing

'Questo codice è stato scritto da Rich Newman, ed è reperibile 
'all'indirizzo Classe HslColor
'E' stato convertito in Vb.Net per metà con un conertitore online free,
'per metà a mano

Public Class HSLColor
    ' Private data members below are on scale 0-1
    ' They are scaled for use externally based on scale
    Private _hue As Double = 1.0
    Private _saturation As Double = 1.0
    Private _luminosity As Double = 1.0

    Private Const scale As Double = 240.0

    Public Property Hue() As Double
        Get
            Return _hue * scale
        End Get
        Set(ByVal Value As Double)
            _hue = CheckRange(Value / scale)
        End Set
    End Property

    Public Property Saturation() As Double
        Get
            Return _saturation * scale
        End Get
        Set(ByVal Value As Double)
            _saturation = CheckRange(Value / scale)
        End Set
    End Property

    Public Property Luminosity() As Double
        Get
            Return _luminosity * scale
        End Get
        Set(ByVal Value As Double)
            _luminosity = CheckRange(Value / scale)
        End Set
    End Property

    Private Function CheckRange(ByVal value As Double) As Double
        If value < 0.0 Then
            value = 0.0
        ElseIf value > 1.0 Then
            value = 1.0
        End If
        Return value
    End Function

    Public Overrides Function ToString() As String
        Return String.Format("H: {0:N2} S: {0:N1} L: {2:N2}", _ 
            Hue, Saturation, Luminosity)
    End Function

    Public Function ToRGBString() As String
        Dim color As Color = CType(Me, Color)
        Return String.Format("R: {0:N2} G: {0:N1} B: {2:N2}", _ 
            color.R, color.G, color.B)
    End Function

    Public Shared Widening Operator CType(ByVal HslColor As HSLColor) _ 
        As Color
        Dim r As Double = 0, g As Double = 0, b As Double = 0
        If HslColor.Luminosity <> 0 Then
            If HslColor.Saturation = 0 Then
                r = g = b = HslColor.Luminosity
            Else
                Dim temp2 As Double = GetTemp2(HslColor)
                Dim temp1 As Double = 2.0 * HslColor.Luminosity - temp2

                r = GetColorComponent(temp1, temp2, _ 
                    HslColor.Hue + 1.0 / 3.0)
                g = GetColorComponent(temp1, temp2, HslColor.Hue)
                b = GetColorComponent(temp1, temp2, _ 
                    HslColor.Hue - 1.0 / 3.0)
            End If
        End If
        Return Color.FromArgb(255, (255 * r), (255 * g), (255 * b))
    End Operator

    'Questa funzione l'ho aggiunta io
    Public Function ToColor() As Color
        Return CType(Me, Color)
    End Function

    Private Shared Function GetColorComponent(ByVal temp1 As Double, _ 
        ByVal temp2 As Double, ByVal temp3 As Double) As Double
        temp3 = MoveIntoRange(temp3)
        If temp3 < 1.0 / 6.0 Then
            Return temp1 + (temp2 - temp1) * 6.0 * temp3
        ElseIf temp3 < 0.5 Then
            Return temp2
        ElseIf temp3 < 2.0 / 3.0 Then
            Return temp1 + ((temp2 - temp1) * ((2.0 / 3.0) - temp3) * 6.0)
        Else
            Return temp1
        End If
    End Function

    Private Shared Function MoveIntoRange(ByVal temp3 As Double) As Double
        If temp3 < 0.0 Then
            temp3 += 1.0
        ElseIf temp3 > 1.0 Then
            temp3 -= 1.0
        End If
        Return temp3
    End Function

    Private Shared Function GetTemp2(ByVal HslColor As HSLColor) As Double
        Dim temp2 As Double
        If HslColor.Luminosity < 0.5 Then
            temp2 = HslColor.Luminosity * (1.0 + HslColor.Saturation)
        Else
            temp2 = HslColor.Luminosity + HslColor.Saturation - _ 
                (HslColor.Luminosity * HslColor.Saturation)
        End If
        Return temp2
    End Function

    Public Shared Widening Operator CType(ByVal color As Color) _ 
        As HSLColor
        Dim hslColor As HSLColor = New HSLColor()
        ' we store hue as 0-1 as opposed to 0-360 
        hslColor.Hue = color.GetHue() / 360.0 
        hslColor.Luminosity = color.GetBrightness()
        hslColor.Saturation = color.GetSaturation()
        Return hslColor
    End Operator

    Public Sub SetRGB(ByVal red As Integer, ByVal green As Integer, _ 
        ByVal blue As Integer)
        Dim hslColor As HSLColor = _ 
            CType(Color.FromArgb(red, green, blue), HSLColor)
        Me.Hue = hslColor.Hue
        Me.Saturation = hslColor.Saturation
        Me.Luminosity = hslColor.Luminosity
    End Sub

    Public Sub New()
    End Sub

    Public Sub New(ByVal color As Color)
        SetRGB(color.R, color.G, color.B)
    End Sub

    Public Sub New(ByVal red As Integer, ByVal green As Integer, _ 
        ByVal blue As Integer)
        SetRGB(red, green, blue)
    End Sub

    Public Sub New(ByVal hue As Double, ByVal saturation As Double, _ 
        ByVal luminosity As Double)
        Me.Hue = hue
        Me.Saturation = saturation
        Me.Luminosity = luminosity
    End Sub

End Class 
E questo è il codice, da usare, per esempio, in un form:
Imports System.Drawing.Drawing2D
Public Class Form1
    'Questa classe rappresenta la barra
    Class GlassBar
        'Colore principale
        Private _Color As Color
        'Area di spazio della barra
        Private _Area As Rectangle

        Public Property Color() As Color
            Get
                Return _Color
            End Get
            Set(ByVal value As Color)
                _Color = value
            End Set
        End Property

        Public Property Area() As Rectangle
            Get
                Return _Area
            End Get
            Set(ByVal value As Rectangle)
                _Area = value
            End Set
        End Property

        'Procedura principale per disegnare
        Public Sub Draw(ByVal G As Graphics)
            'Sfumatura a tre colori: tonalità chiara, normale e chiara
            Dim Blend As New ColorBlend(3)
            'BlendBrush è il pennello che esegue sfumature lineari
            'e tinteggerà lo sfondo passando per i tre colori di Blend
            Dim BlendBrush As LinearGradientBrush
            'MirrorBrush è il pennello che esegue campiture uniformi:
            'creerà il riflesso rimpiendo un'area di bianco trasparente
            Dim MirrorBrush As SolidBrush
            'Questo è il colore della barra convertito in scala HSL
            Dim Col As New HSLColor(Color)
            'OutlinePen è la penna che disegnerà il contorno
            Dim OutlinePen As Pen

            'Per ottenere un colore più chiaro, aumentiamo la
            'luminosità di 54. Siccome la classe è scritta per
            'considerare valori Double da 0 a 1, usiamo la frazione 54/255
            'perchè di solito le gradazioni hanno 256 varianti intermedie
            '(da 0 a 255) proprio come RGB.
            'P.S. per i più solerti: avrete notato che la scala HSL
            'è in 240esimi, ma non fa molta differenza. Potete usare
            'benissimo 54/240
            Col.Luminosity += 54 / 255
            'Imposta i colori della sfumatura: Chiaro, Scuro, Chiaro.
            'ToColor è una funzione che ho aggiunto io che converte
            'un oggetto HslColor in un oggetto Drawing.Color normale
            '(ossia che segue le componenti RGB)
            Blend.Colors = New Color() {Col.ToColor, Color, Col.ToColor}
            'Imposta le posizioni dei colori: all'inizio, a metà, 
            'alla fine
            Blend.Positions = New Single() {0, 0.5, 1}
            'Crea il nuovo pennello: i colori passati nel costruttore sono
            'assolutamente inutili in questo caso, poichè impostando la
            'proprietà InterpolationColors obblighiamo il pennello
            'ad usare la sfumatura di Blend
            BlendBrush = New LinearGradientBrush( _ 
                New Point(Area.X, Area.Y), _ 
                New Point(Area.X, Area.Y + Area.Height), _ 
                Color.White, Color.Blue)
            BlendBrush.InterpolationColors = Blend

            'Diminuisce la luminosità per avere un contorno più scuro
            Col.Luminosity -= 74 / 255
            'Crea la nuova penna di colore scuro, e ampiezza 2pixel.
            OutlinePen = New Pen(Col.ToColor, 2)

            'Crea il pennello per il riflesso. La funzione FromArgb
            'restituisce un oggetto Color fornite in input se sue
            'componenti Alhpa, Red, Green e Blue. Le ultime tre sono 
            'uguali e massime e costituiscono il Bianco puro. 
            'La prima è 90/255 e definisce una trasparenza 
            'abbastanza alta.
            MirrorBrush = New SolidBrush( _ 
                Drawing.Color.FromArgb(90, 255, 255, 255))

            'Disegna tutto
            G.SmoothingMode = SmoothingMode.AntiAlias
            G.FillRectangle(BlendBrush, Area)
            G.FillRectangle(MirrorBrush, _ 
                New Rectangle(Area.X, Area.Y, Area.Width, Area.Height / 2))
            G.DrawRectangle(OutlinePen, Area)
        End Sub
    End Class

    Private Sub Form1_Paint(ByVal sender As Object, _ 
        ByVal e As PaintEventArgs) Handles MyBase.Paint
        'Ecco alcuni esempi di glassbar con colori diversi
        Dim b1, b2, b3, b4 As GlassBar
        b1 = New GlassBar
        b1.Color = Color.DarkRed
        b1.Area = New Rectangle(50, 50, 300, 40)

        b2 = New GlassBar
        b2.Color = Color.Green
        b2.Area = New Rectangle(50, 95, 300, 40)

        b3 = New GlassBar
        b3.Color = Color.Blue
        b3.Area = New Rectangle(50, 140, 300, 40)

        b4 = New GlassBar
        b4.Color = Color.Purple
        b4.Area = New Rectangle(50, 185, 300, 40)

        b1.draw(e.Graphics)
        b2.draw(e.Graphics)
        b3.draw(e.Graphics)
        b4.draw(e.Graphics)
    End Sub
End Class 



Risultato finale






 

The Totem's Lair - Copyright (C) 2009
È vietata la riproduzione sia totale che parziale del sito.