Color: Red, Green, Blue, Hue, Saturation, Brightness, Sector : Color « 2D Graphics « VB.Net Tutorial






Color: Red, Green, Blue, Hue, Saturation, Brightness, Sector
' Quote from
'Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
'by Tim Patrick (Author), John Craig (Author)
'# Publisher: O'Reilly Media, Inc. (September 21, 2006)
'# Language: English
'# ISBN-10: 0596101775
'# ISBN-13: 978-0596101770



Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class ColorRGBSH
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class


Public Class Form1
    Private Sub RBG_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) _
            Handles ValueRed.Scroll, ValueGreen.Scroll, ValueBlue.Scroll
        Dim rgbColor As Color

        rgbColor = Color.FromArgb(0, ValueRed.Value, ValueGreen.Value, ValueBlue.Value)
        ValueHue.Value = CInt(rgbColor.GetHue())
        ValueSaturation.Value = CInt(rgbColor.GetSaturation() * 100.0F)
        ValueBrightness.Value = CInt(rgbColor.GetBrightness() * 100.0F)

        RefreshDisplay()
    End Sub

    Private Sub ValueHue_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) _
            Handles ValueHue.Scroll, ValueSaturation.Scroll, ValueBrightness.Scroll

        Dim useRed As Integer
        Dim useGreen As Integer
        Dim useBlue As Integer
        Dim useHue As Single
        Dim useSaturation As Single
        Dim useBrightness As Single
        Dim hueSector As Integer
        Dim factor As Single
        Dim target1 As Single
        Dim target2 As Single
        Dim target3 As Single

        useHue = CSng(ValueHue.Value)
        useSaturation = CSng(ValueSaturation.Value) / 100.0F
        useBrightness = CSng(ValueBrightness.Value) / 100.0F

        If (useSaturation = 0.0F) Then
            useRed = CInt(useBrightness * 255)
            useGreen = useRed
            useBlue = useRed
        Else
            hueSector = CInt(useHue / 60.0F)
            factor = Math.Abs((useHue / 60.0F) - CSng(hueSector))
            target1 = useBrightness * (1 - useSaturation)
            target2 = useBrightness * (1 - (factor * useSaturation))
            target3 = useBrightness * (1 - ((1 - factor) * useSaturation))

            Select Case hueSector
                Case 0, 6
                    useRed = CInt(useBrightness * 255.0F)
                    useGreen = CInt(target3 * 255.0F)
                    useBlue = CInt(target1 * 255.0F)
                Case 1
                    useRed = CInt(target2 * 255.0F)
                    useGreen = CInt(useBrightness * 255.0F)
                    useBlue = CInt(target1 * 255.0F)
                Case 2
                    useRed = CInt(target1 * 255.0F)
                    useGreen = CInt(useBrightness * 255.0F)
                    useBlue = CInt(target3 * 255.0F)
                Case 3
                    useRed = CInt(target1 * 255.0F)
                    useGreen = CInt(target2 * 255.0F)
                    useBlue = CInt(useBrightness * 255.0F)
                Case 4
                    useRed = CInt(target3 * 255.0F)
                    useGreen = CInt(target1 * 255.0F)
                    useBlue = CInt(useBrightness * 255.0F)
                Case 5
                    useRed = CInt(useBrightness * 255.0F)
                    useGreen = CInt(target1 * 255.0F)
                    useBlue = CInt(target2 * 255.0F)
            End Select
        End If

        ValueRed.Value = useRed
        ValueGreen.Value = useGreen
        ValueBlue.Value = useBlue

        RefreshDisplay()
    End Sub

    Private Sub RefreshDisplay()
        NumberRed.Text = CStr(ValueRed.Value)
        NumberGreen.Text = CStr(ValueGreen.Value)
        NumberBlue.Text = CStr(ValueBlue.Value)
        NumberHue.Text = CStr(ValueHue.Value)
        NumberSaturation.Text = Format(CDec(ValueSaturation.Value) / 100@, "0.00")
        NumberBrightness.Text = Format(CDec(ValueBrightness.Value) / 100@, "0.00")

        ShowColor.BackColor = Color.FromArgb(255, ValueRed.Value, _
            ValueGreen.Value, ValueBlue.Value)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RBG_Scroll(ValueRed, New Windows.Forms.ScrollEventArgs(ScrollEventType.EndScroll, 0))
    End Sub
End Class


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.ShowColor = New System.Windows.Forms.PictureBox
        Me.LabelRed = New System.Windows.Forms.Label
        Me.ValueRed = New System.Windows.Forms.HScrollBar
        Me.NumberRed = New System.Windows.Forms.Label
        Me.NumberGreen = New System.Windows.Forms.Label
        Me.ValueGreen = New System.Windows.Forms.HScrollBar
        Me.LabelGreen = New System.Windows.Forms.Label
        Me.NumberBlue = New System.Windows.Forms.Label
        Me.ValueBlue = New System.Windows.Forms.HScrollBar
        Me.LabelBlue = New System.Windows.Forms.Label
        Me.NumberBrightness = New System.Windows.Forms.Label
        Me.ValueBrightness = New System.Windows.Forms.HScrollBar
        Me.LabelBrightness = New System.Windows.Forms.Label
        Me.NumberSaturation = New System.Windows.Forms.Label
        Me.ValueSaturation = New System.Windows.Forms.HScrollBar
        Me.LabelSaturation = New System.Windows.Forms.Label
        Me.NumberHue = New System.Windows.Forms.Label
        Me.ValueHue = New System.Windows.Forms.HScrollBar
        Me.LabelHue = New System.Windows.Forms.Label
        CType(Me.ShowColor, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'ShowColor
        '
        Me.ShowColor.BackColor = System.Drawing.SystemColors.Control
        Me.ShowColor.Location = New System.Drawing.Point(8, 80)
        Me.ShowColor.Name = "ShowColor"
        Me.ShowColor.Size = New System.Drawing.Size(336, 32)
        Me.ShowColor.TabIndex = 1
        Me.ShowColor.TabStop = False
        '
        'LabelRed
        '
        Me.LabelRed.AutoSize = True
        Me.LabelRed.Location = New System.Drawing.Point(8, 10)
        Me.LabelRed.Name = "LabelRed"
        Me.LabelRed.Size = New System.Drawing.Size(27, 13)
        Me.LabelRed.TabIndex = 0
        Me.LabelRed.Text = "Red"
        '
        'ValueRed
        '
        Me.ValueRed.LargeChange = 1
        Me.ValueRed.Location = New System.Drawing.Point(72, 8)
        Me.ValueRed.Maximum = 255
        Me.ValueRed.Name = "ValueRed"
        Me.ValueRed.Size = New System.Drawing.Size(224, 16)
        Me.ValueRed.TabIndex = 1
        '
        'NumberRed
        '
        Me.NumberRed.AutoSize = True
        Me.NumberRed.Location = New System.Drawing.Point(304, 10)
        Me.NumberRed.Name = "NumberRed"
        Me.NumberRed.Size = New System.Drawing.Size(13, 13)
        Me.NumberRed.TabIndex = 2
        Me.NumberRed.Text = "0"
        '
        'NumberGreen
        '
        Me.NumberGreen.AutoSize = True
        Me.NumberGreen.Location = New System.Drawing.Point(304, 34)
        Me.NumberGreen.Name = "NumberGreen"
        Me.NumberGreen.Size = New System.Drawing.Size(13, 13)
        Me.NumberGreen.TabIndex = 5
        Me.NumberGreen.Text = "0"
        '
        'ValueGreen
        '
        Me.ValueGreen.LargeChange = 1
        Me.ValueGreen.Location = New System.Drawing.Point(72, 32)
        Me.ValueGreen.Maximum = 255
        Me.ValueGreen.Name = "ValueGreen"
        Me.ValueGreen.Size = New System.Drawing.Size(224, 16)
        Me.ValueGreen.TabIndex = 4
        '
        'LabelGreen
        '
        Me.LabelGreen.AutoSize = True
        Me.LabelGreen.Location = New System.Drawing.Point(8, 34)
        Me.LabelGreen.Name = "LabelGreen"
        Me.LabelGreen.Size = New System.Drawing.Size(36, 13)
        Me.LabelGreen.TabIndex = 3
        Me.LabelGreen.Text = "Green"
        '
        'NumberBlue
        '
        Me.NumberBlue.AutoSize = True
        Me.NumberBlue.Location = New System.Drawing.Point(304, 58)
        Me.NumberBlue.Name = "NumberBlue"
        Me.NumberBlue.Size = New System.Drawing.Size(13, 13)
        Me.NumberBlue.TabIndex = 8
        Me.NumberBlue.Text = "0"
        '
        'ValueBlue
        '
        Me.ValueBlue.LargeChange = 1
        Me.ValueBlue.Location = New System.Drawing.Point(72, 56)
        Me.ValueBlue.Maximum = 255
        Me.ValueBlue.Name = "ValueBlue"
        Me.ValueBlue.Size = New System.Drawing.Size(224, 16)
        Me.ValueBlue.TabIndex = 7
        '
        'LabelBlue
        '
        Me.LabelBlue.AutoSize = True
        Me.LabelBlue.Location = New System.Drawing.Point(8, 58)
        Me.LabelBlue.Name = "LabelBlue"
        Me.LabelBlue.Size = New System.Drawing.Size(28, 13)
        Me.LabelBlue.TabIndex = 6
        Me.LabelBlue.Text = "Blue"
        '
        'NumberBrightness
        '
        Me.NumberBrightness.AutoSize = True
        Me.NumberBrightness.Location = New System.Drawing.Point(304, 170)
        Me.NumberBrightness.Name = "NumberBrightness"
        Me.NumberBrightness.Size = New System.Drawing.Size(13, 13)
        Me.NumberBrightness.TabIndex = 17
        Me.NumberBrightness.Text = "0"
        '
        'ValueBrightness
        '
        Me.ValueBrightness.LargeChange = 1
        Me.ValueBrightness.Location = New System.Drawing.Point(72, 168)
        Me.ValueBrightness.Name = "ValueBrightness"
        Me.ValueBrightness.Size = New System.Drawing.Size(224, 16)
        Me.ValueBrightness.TabIndex = 16
        '
        'LabelBrightness
        '
        Me.LabelBrightness.AutoSize = True
        Me.LabelBrightness.Location = New System.Drawing.Point(8, 170)
        Me.LabelBrightness.Name = "LabelBrightness"
        Me.LabelBrightness.Size = New System.Drawing.Size(56, 13)
        Me.LabelBrightness.TabIndex = 15
        Me.LabelBrightness.Text = "Brightness"
        '
        'NumberSaturation
        '
        Me.NumberSaturation.AutoSize = True
        Me.NumberSaturation.Location = New System.Drawing.Point(304, 146)
        Me.NumberSaturation.Name = "NumberSaturation"
        Me.NumberSaturation.Size = New System.Drawing.Size(13, 13)
        Me.NumberSaturation.TabIndex = 14
        Me.NumberSaturation.Text = "0"
        '
        'ValueSaturation
        '
        Me.ValueSaturation.LargeChange = 1
        Me.ValueSaturation.Location = New System.Drawing.Point(72, 144)
        Me.ValueSaturation.Name = "ValueSaturation"
        Me.ValueSaturation.Size = New System.Drawing.Size(224, 16)
        Me.ValueSaturation.TabIndex = 13
        '
        'LabelSaturation
        '
        Me.LabelSaturation.AutoSize = True
        Me.LabelSaturation.Location = New System.Drawing.Point(8, 146)
        Me.LabelSaturation.Name = "LabelSaturation"
        Me.LabelSaturation.Size = New System.Drawing.Size(55, 13)
        Me.LabelSaturation.TabIndex = 12
        Me.LabelSaturation.Text = "Saturation"
        '
        'NumberHue
        '
        Me.NumberHue.AutoSize = True
        Me.NumberHue.Location = New System.Drawing.Point(304, 122)
        Me.NumberHue.Name = "NumberHue"
        Me.NumberHue.Size = New System.Drawing.Size(13, 13)
        Me.NumberHue.TabIndex = 11
        Me.NumberHue.Text = "0"
        '
        'ValueHue
        '
        Me.ValueHue.LargeChange = 1
        Me.ValueHue.Location = New System.Drawing.Point(72, 120)
        Me.ValueHue.Maximum = 360
        Me.ValueHue.Name = "ValueHue"
        Me.ValueHue.Size = New System.Drawing.Size(224, 16)
        Me.ValueHue.TabIndex = 10
        '
        'LabelHue
        '
        Me.LabelHue.AutoSize = True
        Me.LabelHue.Location = New System.Drawing.Point(8, 122)
        Me.LabelHue.Name = "LabelHue"
        Me.LabelHue.Size = New System.Drawing.Size(27, 13)
        Me.LabelHue.TabIndex = 9
        Me.LabelHue.Text = "Hue"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(354, 195)
        Me.Controls.Add(Me.NumberBrightness)
        Me.Controls.Add(Me.ValueBrightness)
        Me.Controls.Add(Me.LabelBrightness)
        Me.Controls.Add(Me.NumberSaturation)
        Me.Controls.Add(Me.ValueSaturation)
        Me.Controls.Add(Me.LabelSaturation)
        Me.Controls.Add(Me.NumberHue)
        Me.Controls.Add(Me.ValueHue)
        Me.Controls.Add(Me.LabelHue)
        Me.Controls.Add(Me.NumberBlue)
        Me.Controls.Add(Me.ValueBlue)
        Me.Controls.Add(Me.LabelBlue)
        Me.Controls.Add(Me.NumberGreen)
        Me.Controls.Add(Me.ValueGreen)
        Me.Controls.Add(Me.LabelGreen)
        Me.Controls.Add(Me.NumberRed)
        Me.Controls.Add(Me.ValueRed)
        Me.Controls.Add(Me.LabelRed)
        Me.Controls.Add(Me.ShowColor)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "RBG and HSV/HSB"
        CType(Me.ShowColor, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents ShowColor As System.Windows.Forms.PictureBox
    Friend WithEvents LabelRed As System.Windows.Forms.Label
    Friend WithEvents ValueRed As System.Windows.Forms.HScrollBar
    Friend WithEvents NumberRed As System.Windows.Forms.Label
    Friend WithEvents NumberGreen As System.Windows.Forms.Label
    Friend WithEvents ValueGreen As System.Windows.Forms.HScrollBar
    Friend WithEvents LabelGreen As System.Windows.Forms.Label
    Friend WithEvents NumberBlue As System.Windows.Forms.Label
    Friend WithEvents ValueBlue As System.Windows.Forms.HScrollBar
    Friend WithEvents LabelBlue As System.Windows.Forms.Label
    Friend WithEvents NumberBrightness As System.Windows.Forms.Label
    Friend WithEvents ValueBrightness As System.Windows.Forms.HScrollBar
    Friend WithEvents LabelBrightness As System.Windows.Forms.Label
    Friend WithEvents NumberSaturation As System.Windows.Forms.Label
    Friend WithEvents ValueSaturation As System.Windows.Forms.HScrollBar
    Friend WithEvents LabelSaturation As System.Windows.Forms.Label
    Friend WithEvents NumberHue As System.Windows.Forms.Label
    Friend WithEvents ValueHue As System.Windows.Forms.HScrollBar
    Friend WithEvents LabelHue As System.Windows.Forms.Label

End Class








17.26.Color
17.26.1.KnownColor.ActiveBorder
17.26.2.ColorTranslator.FromHtml('#0000ff')
17.26.3.Color.FromNameColor.FromName
17.26.4.Color.FromArgbColor.FromArgb
17.26.5.Create Color from R G B values
17.26.6.Create Color from R G B value and Alpha
17.26.7.Create Color from SystemColors
17.26.8.Color AlphaColor Alpha
17.26.9.Convert color from string (FF00FF)Convert color from string (FF00FF)
17.26.10.Color.FromWin32Color.FromWin32
17.26.11.Color.FromHtmlColor.FromHtml
17.26.12.Get Color from static fields defined in ColorGet Color from static fields defined in Color
17.26.13.Color.FromArgb(-1090518785)Color.FromArgb(-1090518785)
17.26.14.Get Hue, Saturation, Brightness from a colorGet Hue, Saturation, Brightness from a color
17.26.15.Using different colors in Visual BasicUsing different colors in Visual Basic
17.26.16.Color: Red, Green, Blue, Hue, Saturation, Brightness, SectorColor: Red, Green, Blue, Hue, Saturation, Brightness, Sector
17.26.17.System.Drawing.ColorTranslator.FromOleSystem.Drawing.ColorTranslator.FromOle
17.26.18.Transparent ColorTransparent Color
17.26.19.Alpha blending colorAlpha blending color