Я занимался созданием цветового круга на основе HSL в Excel VBA, используя крошечные ячейки в качестве «пикселей», и это оказалось довольно неплохо, поэтому я решил поделиться.
Это демонстрирует преобразование между HSL и RGB , а также как программно рисовать линии / круги на любой сетке - даже ячейки электронной таблицы.
Код готов к запуску как есть:
Option Explicit
Const colorSheetName = "COLORS"
Const pi = 3.14159265358979
Const squareSize = 3.75 'cell square size (pts)
Const cDiameter = 80# 'circle diameter (cells)
Const numAngles = 360# 'number of angles (lines to draw)
Sub CalculateColorWheel()
Dim ws As Worksheet, radsPerAngle As Double, radius As Long, xStop As Double, _
yStop As Double, z As Integer, xyLength As Double, lineDot As Long, _
lineLength As Long, h As Byte, s As Byte, v As Byte, r As Byte, g As Byte, b As Byte
Set ws = ThisWorkbook.Sheets.Add 'create new worksheet
On Error Resume Next 'ignore error
Application.DisplayAlerts = False 'ignore warning
ThisWorkbook.Sheets(colorSheetName).Delete 'delete worksheet (if exists)
Application.DisplayAlerts = True 'stop ignoring warnings
On Error GoTo 0 'stop ignoring errors
With ws
.Name = colorSheetName 'name the new sheet
.Rows.RowHeight = squareSize 'set rowheight
.Columns.ColumnWidth=widthToColumnWidth(squareSize) 'match columnwidth to row
ActiveWindow.DisplayGridlines = False 'hide gridlines
ActiveWindow.DisplayHeadings = False 'hide row/col headings
radius = cDiameter / 2 'centre point
lineLength = radius * 1.5 'dots per angle (line)
radsPerAngle = (360 / numAngles) * pi / 180 'radians=a(degrees)×pi÷180°
Debug.Print "Grid size=" & .[a1].Height & "×" & .[a1].Width _
& ", Diameter:" & cDiameter _
& ", Area=" & Round(pi * radius ^ 2, 0) _
& ", Circumference=" & Round(2 * pi * radius, 0) _
& ", Radians per Angle=" & Round(radsPerAngle, 3) _
& " × " & numAngles & " angles" 'stats
For z = 0 To numAngles - 1 'loop through each angle
For lineDot = 1 To lineLength 'loop thru length of line
xyLength = radius * (lineDot / lineLength) 'calc dot xy& offset top-left
xStop = Int(Cos(radsPerAngle * z) * xyLength) + radius + 2 'x (column)
yStop = Int(Sin(radsPerAngle * z) * xyLength) + radius + 2 'y (row)
If .Cells(yStop, xStop).Interior.Pattern=xlNone Then 'skip colored cells
h = ((z + 1) / numAngles) * 255 'hue=angle
s = (lineDot / lineLength) * 255 'saturation=radius
v = 255 'maximum brightness. (Adjustable)
HSVtoRGB h, s, v, r, g, b 'convert HSV to RGB
.Cells(yStop, xStop).Interior.Color=rgb(r,g,b) 'color the cell
dots = dots + 1
End If
Next lineDot
Application.StatusBar = Format(z / (numAngles - 1), "0%")
DoEvents 'don't lag
Next z
End With
Beep
Application.StatusBar = "Finished drawing color circle (" & dots & " colors)"
End Sub
Public Function widthToColumnWidth(pts As Double) As Double
'convert desired column width (points) to Excel "ColWidthUnits"
'12pts and under is a 1:12 ratio of (colWidthUnits:Pts).
' Over 12pts: 1:12 for 1st unit, then 1:(75/11) for remainder
Select Case pts
Case Is <= 0: widthToColumnWidth = 0
Case Is <= 12: widthToColumnWidth = pts / 12
Case Else: widthToColumnWidth = 1 + (pts - 12) / (75 / 11) '
End Select
End Function
Public Sub HSVtoRGB(h As Byte, s As Byte, v As Byte, r As Byte, g As Byte, b As Byte)
Dim minV As Byte, maxV As Byte, Chroma As Byte, tempH As Double
If v = 0 Then
r = 0: g = 0: b = 0
Else
If s = 0 Then
r = v: g = v: b = v:
Else
maxV = v: Chroma = s / 255 * maxV: minV = maxV - Chroma
Select Case h
Case Is >= 170: tempH = (h - 170) / 43: g = 0
If tempH < 1 Then
b = maxV: r = maxV * tempH
Else: r = maxV: b = maxV * (2 - tempH): End If
Case Is >= 85: tempH = (h - 85) / 43: r = 0
If tempH < 1 Then
g = maxV: b = maxV * tempH
Else: b = maxV: g = maxV * (2 - tempH): End If
Case Else: tempH = h / 43: b = 0
If tempH < 1 Then
r = maxV: g = maxV * tempH
Else: g = maxV: r = maxV * (2 - tempH): End If
End Select
r = r / maxV * (maxV - minV) + minV
g = g / maxV * (maxV - minV) + minV
b = b / maxV * (maxV - minV) + minV
End If
End If
End Sub
Как запустить это в Excel: Вставьте приведенный выше код и вставьте в обычный модуль . (Выберите код, Ctrl + C для копирования, затем в Excel, удерживайте Alt и нажмите F11 + I + M , а затем Ctrl + V для вставки и F5 для запуска.) ?
Дополнительная информация: