Click here to Skip to main content
15,896,557 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.9K   15.8K   440  
Understand and use color models in .NET
''' <summary>
''' Structure to define HSB.
''' </summary>
Public Structure HSB

#Region "Fields"

    Private _Hue As Double
    Private _Saturation As Double
    Private _Brightness As Double

#End Region

#Region "Operators"

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

    Public Shared Operator <>(ByVal item1 As HSB, ByVal item2 As HSB) As Boolean
        Return item1.Hue <> item2.Hue OrElse _
               item1.Saturation <> item2.Saturation OrElse _
               item1.Brightness <> item2.Brightness
    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 Brightness()
        Get
            Return _Brightness
        End Get
        Set(ByVal value)
            If value > 1 Then
                _Brightness = 1
            Else
                If value < 0 Then
                    _Brightness = 0
                Else
                    _Brightness = value
                End If
            End If
        End Set
    End Property

#End Region

    ''' <summary>
    ''' Creates an instance of a HSB 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="b">Brightness value (must be between 0 and 1).</param>
    Public Sub New(ByVal h As Double, ByVal s As Double, ByVal b As Double)
        Me.Hue = h
        Me.Saturation = s
        Me.Brightness = b
    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, HSB))
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return Hue.GetHashCode() ^ Saturation.GetHashCode() ^ Brightness.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