Click here to Skip to main content
15,895,833 members
Articles / Programming Languages / C#

Manipulating colors in .NET - Part 1

Rate me:
Please Sign up or sign in to vote.
4.96/5 (275 votes)
3 Jun 2007CPOL16 min read 547.7K   15.8K   440  
Understand and use color models in .NET
''' <summary>
''' Structure to define HSL.
''' </summary>
Public Structure HSL

#Region "Fields"

    Private _Hue As Double
    Private _Saturation As Double
    Private _Luminance As Double

#End Region

#Region "Operators"

    Public Shared Operator =(ByVal item1 As HSL, ByVal item2 As HSL) As Boolean
        Return item1.Hue = item2.Hue AndAlso _
               item1.Saturation = item2.Saturation AndAlso _
               item1.Luminance = item2.Luminance
    End Operator

    Public Shared Operator <>(ByVal item1 As HSL, ByVal item2 As HSL) As Boolean
        Return item1.Hue <> item2.Hue OrElse _
               item1.Saturation <> item2.Saturation OrElse _
               item1.Luminance <> item2.Luminance
    End Operator

#End Region

#Region "Accessors"

    Public Property Hue()
        Get
            Return _Hue
        End Get
        Set(ByVal value)
            If value > 360 Then
                _Hue = 360
            Else
                If value < 0 Then
                    _Hue = 0
                Else
                    _Hue = value
                End If
            End If
        End Set
    End Property

    Public Property Saturation()
        Get
            Return _Saturation
        End Get
        Set(ByVal value)
            If value > 1 Then
                _Saturation = 1
            Else
                If value < 0 Then
                    _Saturation = 0
                Else
                    _Saturation = value
                End If
            End If
        End Set
    End Property

    Public Property Luminance()
        Get
            Return _Luminance
        End Get
        Set(ByVal value)
            If value > 1 Then
                _Luminance = 1
            Else
                If value < 0 Then
                    _Luminance = 0
                Else
                    _Luminance = value
                End If
            End If
        End Set
    End Property

#End Region

    ''' <summary>
    ''' Creates an instance of a HSL structure.
    ''' </summary>
    ''' <param name="h">Hue value (must be between 0 and 360).</param>
    ''' <param name="s">Saturation value (must be between 0 and 1).</param>
    ''' <param name="l">Luminance value (must be between 0 and 1).</param>
    Public Sub New(ByVal h As Double, ByVal s As Double, ByVal l As Double)
        Me.Hue = h
        Me.Saturation = s
        Me.Luminance = l
    End Sub

#Region "Methods"

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        If (obj Is Nothing) Or (Me.GetType() IsNot obj.GetType()) Then Return False
        Return (Me = CType(obj, HSL))
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return Hue.GetHashCode() ^ Saturation.GetHashCode() ^ Luminance.GetHashCode()
    End Function

#End Region

End Structure

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
France France
IT consultant and Project Manager in Paris, specialized in software engineering/design.

He spends most of his time in meetings Smile | :)
He would love to have more time to develop all those ideas/concepts he has in mind.

Comments and Discussions