在VBA中绘制RGB光谱图

时间:2017-07-03 16:24:01

标签: vba colors

我正在开展一个小项目,我希望能够“绘制”RGB Spectrum。我想创建一个完整“光谱”的视觉表示。我通过对每种颜色(R,G和B)使用3个正弦函数来做到这一点,但是,我的程序创建的图形并不代表全谱(我不是在寻找完整的1680万种颜色,但是我的图形是缺乏橙色/黄色和其他一些色调)。以下是我到目前为止的情况。

Dim mul As Double, N As Double, i As Double
mul = 12
N = 3.14 * mul
For i = 1 To 10 Step 3.14 / 100
    Dim r As Double, b As Double, g As Double
    r = Sin(i)
    g = Sin(-1 * i)
    b = Cos(i)
    If r <= 0 Then r = 0
    If g <= 0 Then g = 0
    If b <= 0 Then b = 0
    Debug.Print Sin(i) * 250 & 1
    Cells(10, i * 10).Interior.Color = RGB(r * 255, g * 255, b * 255)
    Cells(11, i * 10).Interior.Color = RGB(r * 255, 0, 0)
    Cells(12, i * 10).Interior.Color = RGB(0, g * 255, 0)
    Cells(13, i * 10).Interior.Color = RGB(0, 0, b * 255)


Next i

1 个答案:

答案 0 :(得分:1)

If r <= 0 Then r = 0这样的行使一半的值映射到零

变体,将rgb值视为正弦波,平均值为127.5,幅度为127.5(所以他们从0到255)是:

Sub test()
    Dim mul As Double, N As Double, i As Double
    Dim r As Long, b As Long, g As Long

    For i = 1 To 10 Step Application.Pi / 100
        r = Round(127.5 + 127.5 * Sin(i))
        g = Round(127.5 + 127.5 * Sin(-1 * i))
        b = Round(127.5 + 127.5 * Cos(i))
        Cells(10, i * 10).Interior.Color = RGB(r, g, b)
        Cells(11, i * 10).Interior.Color = RGB(r, 0, 0)
        Cells(12, i * 10).Interior.Color = RGB(0, g, 0)
        Cells(13, i * 10).Interior.Color = RGB(0, 0, b)
    Next i
End Sub

下图显示了您的代码(前一个)和修改后的代码生成的频谱。修改后的代码确实涵盖了更多的频谱:

enter image description here

相关问题