65.9K
CodeProject is changing. Read more.
Home

Pick Color

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (9 votes)

May 14, 2003

viewsIcon

98682

downloadIcon

1231

A fun color picker in ASP.NET

Sample Image - Pick_Color.jpg

Introduction

This is an amusing way in order to select a color from a page web.

Using the code

As it is knows that the coordinates of a color comes from Red Blue Green (RGB), in order to create the intentional shadings we must convert to Luninosità tone Saturation (HLS). The function is as follows :-

   Private Sub Paletta(ByVal TargetForm As String, _
     ByVal PalettaRow As Long, _
     ByVal Saturation As Long)
        Dim RGBColor As String
        Dim Gray As Double
        Dim lum As Double
        Dim sat As Double
        Dim hue As Double
        Dim row As HtmlTableRow
        Dim cell As HtmlTableCell
        Dim RBGcolor As String

        sat = Saturation / 100
        For hue = 0 To 359 Step (360 / PalettaRow)
            row = New HtmlTableRow()
            For lum = 0.1 To 1 Step 0.02
                cell = New HtmlTableCell()
                HlsToRgb(hue, lum, sat, RGBColor)
                cell.BgColor = RGBColor
                cell.Controls.Add(New HyperLink())
                DirectCast(cell.Controls(0), HyperLink).Text = "_"
                DirectCast(cell.Controls(0), HyperLink).ForeColor = _
                    Color.FromArgb(Val("&h" & Mid(RGBColor, 1, 2)), _
                    Val("&h" & Mid(RGBColor, 3, 2)), _
                    Val("&h" & Mid(RGBColor, 5, 2)))
                DirectCast(cell.Controls(0), HyperLink).NavigateUrl = _
                    TargetForm & "?&C=" & RGBColor
                row.Cells.Add(cell)
            Next lum
            Table1.Rows.Add(row)
        Next hue
        row = New HtmlTableRow()
        For Gray = 0 To 255 Step 5.7
            cell = New HtmlTableCell()
            cell.BgColor = Right("00" & Hex(Gray), 2) & _
               Right("00" & Hex(Gray), 2) & Right("00" & Hex(Gray), 2)
            cell.Controls.Add(New HyperLink())
            DirectCast(cell.Controls(0), HyperLink).Text = "_"
            DirectCast(cell.Controls(0), HyperLink).ForeColor = _
               Color.FromArgb(Gray, Gray, Gray)
            DirectCast(cell.Controls(0), HyperLink).NavigateUrl = _
               TargetForm & "?&C=" & Right("00" & Hex(Gray), 2) & _
               Right("00" & Hex(Gray), 2) & Right("00" & Hex(Gray), 2)
            row.Cells.Add(cell)
        Next Gray
        Table1.Rows.Add(row)
    End Sub
    
    ' Converte HLS in RGB
    Private Sub HlsToRgb(ByVal H As Double, ByVal L As Double, _
          ByVal S As Double, ByRef RGBColor As String)
        Dim p1 As Double
        Dim p2 As Double
        Dim r As Double
        Dim g As Double
        Dim b As Double

        If L <= 0.5 Then
            p2 = L * (1 + S)
        Else
            p2 = L + S - L * S
        End If
        p1 = 2 * L - p2
        If S = 0 Then
            r = Int(L * 255)
            g = Int(L * 255)
            b = Int(L * 255)
        Else
            r = Int(QqhToRgb(p1, p2, H + 120) * 255)
            g = Int(QqhToRgb(p1, p2, H) * 255)
            b = Int(QqhToRgb(p1, p2, H - 120) * 255)
        End If
        RGBColor = Right("00" & Hex(r), 2) & Right("00" & Hex(g), 2) & _
            Right("00" & Hex(b), 2)
    End Sub

The function Paletta previews the page where we give back the result, given the number of lines that composes the table and the brightness (that translates to "full" color or complete gray). After we have printed the table, and have selected the proposed color, the result is collected through :

Request.QueryString("C")

Points of Interest

This code has been translated from my site Web originally in ASP, and served to modify all the elements of the page web (Links, tables, etc.)

History

This is the first version of the Pick Color, but I have some changes already in mind for even more fun... Bye bye :)