楼主 kevinchengcw |
对于RGB的关系,本文附件提供了一个演示效果,相信对了解RGB颜色之间的关系会有所帮助。 图示是用VBA代码生成,代码如下:- Sub test() '生成RGB三色对应条的代码
- For N = 1 To 7
- Cells(1, N) = " R, G, B : 当前色值" '写标题行
- Next N
- For N = 2 To 257 '单元格及RGB值(n-2)范围
- If N < 129 Then Rows(N).Font.Color = vbWhite '当颜色较深时使用白色字体
- Cells(N, 1).Interior.Color = RGB(N - 2, 0, 0)
- Cells(N, 1) = Space(3 - Len(N - 2)) & N - 2 & ", 0, 0 : " & Cells(N, 1).Interior.Color & Space(8 - Len(Cells(N, 1).Interior.Color)) '格式化输出显示内容
- Cells(N, 2).Interior.Color = RGB(0, N - 2, 0)
- Cells(N, 2) = " 0, " & Space(3 - Len(N - 2)) & N - 2 & ", 0 : " & Cells(N, 2).Interior.Color & Space(8 - Len(Cells(N, 2).Interior.Color))
- Cells(N, 3).Interior.Color = RGB(0, 0, N - 2)
- Cells(N, 3).Font.Color = vbWhite
- Cells(N, 3) = " 0, 0, " & Space(3 - Len(N - 2)) & N - 2 & " : " & Cells(N, 3).Interior.Color & Space(8 - Len(Cells(N, 3).Interior.Color))
- Cells(N, 4).Interior.Color = RGB(N - 2, N - 2, 0)
- Cells(N, 4) = Space(3 - Len(N - 2)) & N - 2 & ", " & Space(3 - Len(N - 2)) & N - 2 & ", 0 : " & Cells(N, 4).Interior.Color & Space(8 - Len(Cells(N, 4).Interior.Color))
- Cells(N, 5).Interior.Color = RGB(N - 2, 0, N - 2)
- Cells(N, 5) = Space(3 - Len(N - 2)) & N - 2 & ", 0, " & Space(3 - Len(N - 2)) & N - 2 & " : " & Cells(N, 5).Interior.Color & Space(8 - Len(Cells(N, 5).Interior.Color))
- Cells(N, 6).Interior.Color = RGB(0, N - 2, N - 2)
- Cells(N, 6) = " 0, " & Space(3 - Len(N - 2)) & N - 2 & ", " & Space(3 - Len(N - 2)) & N - 2 & " : " & Cells(N, 6).Interior.Color & Space(8 - Len(Cells(N, 6).Interior.Color))
- Cells(N, 7).Interior.Color = RGB(N - 2, N - 2, N - 2)
- Cells(N, 7) = Space(3 - Len(N - 2)) & N - 2 & ", " & Space(3 - Len(N - 2)) & N - 2 & ", " & Space(3 - Len(N - 2)) & N - 2 & " : " & Cells(N, 7).Interior.Color & Space(8 - Len(Cells(N, 7).Interior.Color))
- Next N
- Columns("A:G").EntireColumn.AutoFit '列宽自适应
- End Sub
- Sub test2() '生成随机色彩及对应值的代码
- Dim R, G, B, M, N As Integer
- Cells.Delete '先清空区域
- For N = 1 To 7
- Cells(1, N) = " R, G, B : 当前色值" '生成标题行
- Next N
- For N = 2 To 100
- For M = 1 To 7
- '取得R,G,B的随机值
- R = Int((Rnd * 10000) Mod 256)
- G = Int((Rnd * 10000) Mod 256)
- B = Int((Rnd * 10000) Mod 256)
- Cells(N, M).Interior.Color = RGB(R, G, B)
- Cells(N, M) = Space(3 - Len(R)) & R & ", " & Space(3 - Len(G)) & G & ", " & Space(3 - Len(B)) & B & " : " & Cells(N, M).Interior.Color & Space(8 - Len(Cells(N, M).Interior.Color))
- Next M
- Next N
- Columns("A:G").EntireColumn.AutoFit
- End Sub
RGB色彩学习.rar |