循环遍历所有可能的RGB组合

时间:2017-09-10 03:57:31

标签: vb.net

我想尝试做的是将 form2 BackColor(点击一个复选框)改为平滑淡出所有可能的颜色,所以从RGB:0 ,0,0一直到255,255,255。谢谢。

这就是我正在做的事情:

Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
    Dim lngR As Long
    Dim lngG As Long
    Dim lngB As Long

    For lngR = 0 To 255
        For lngG = 0 To 255
            For lngB = 0 To 255
                Me.BackColor = RGB(lngR, lngG, lngB)
                Sleep(30) 'change this value to change speed
            Next lngB
        Next lngG
    Next lngR
End Sub

我得到的错误是:Value of Integer cannot be converted to Color.

1 个答案:

答案 0 :(得分:0)

通过在更改背面颜色后添加Refresh()来改变颜色。这会使循环减慢到足以看到颜色变化,但是你循环大约1660万次(如果你不是一直重新绘制非常快,但重绘非常慢)并且会导致用户入睡。

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim intR As Int32
        Dim intG As Int32
        Dim intB As Int32
        'Alpha channel is implicitly opaque (255))
        Dim x As Integer = 0
        For intR = 0 To 254 Step 20
            For intG = 0 To 254 Step 20
                For intB = 0 To 254 Step 20
                    Threading.Thread.Sleep(10) 'change this value to change speed, 500 is 1/2 second
                    '16.6 million loops Wow - cut that down by using Step
                    Me.BackColor = Color.FromArgb(intR, intG, intB)
                    Refresh()
                    x += 1
                Next
            Next intG
        Next intR
        MessageBox.Show($"Red = {intR}, Green = {intG}, Blue = {intB}{vbCrLf}The value of x is {x}")
    End Sub