P i*_*P i 13 math color-picker colors color-wheel
我想创建一个包含12个切片的饼图,每个切片都有不同的颜色.
几乎每个色轮似乎都遵循相同的格式; 例如:http://www.tigercolor.com/color-lab/color-theory/color-theory-intro.htm.
但是有什么算法可以产生颜色?RGB(theta)背后的数学是什么?当然必须有一些既定的科学,但谷歌并没有给我任何线索.
看看http://www.easyrgb.com它有许多颜色转换背后的算法.这是RGB - > HSV之一.
var_R = ( R / 255 ) //RGB from 0 to 255
var_G = ( G / 255 )
var_B = ( B / 255 )
var_Min = min( var_R, var_G, var_B ) //Min. value of RGB
var_Max = max( var_R, var_G, var_B ) //Max. value of RGB
del_Max = var_Max - var_Min //Delta RGB value
V = var_Max
if ( del_Max == 0 ) //This is a gray, no chroma...
{
H = 0 //HSV results from 0 to 1
S = 0
}
else //Chromatic data...
{
S = del_Max / var_Max
del_R = ( ( ( var_Max - var_R ) / 6 ) + ( del_Max / 2 ) ) / del_Max
del_G = ( ( ( var_Max - var_G ) / 6 ) + ( del_Max / 2 ) ) / del_Max
del_B = ( ( ( var_Max - var_B ) / 6 ) + ( del_Max / 2 ) ) / del_Max
if ( var_R == var_Max ) H = del_B - del_G
else if ( var_G == var_Max ) H = ( 1 / 3 ) + del_R - del_B
else if ( var_B == var_Max ) H = ( 2 / 3 ) + del_G - del_R
if ( H < 0 ) H += 1
if ( H > 1 ) H -= 1
}
Run Code Online (Sandbox Code Playgroud)
色轮(如Mac OS X颜色选择器,如下图所示)显示色调和饱和度(HSV色彩空间中三个组件中的两个).色调随角度变化,饱和度随半径变化.通常,该值(即亮度)有一个单独的滑块.

有关如何在HSV和RGB之间来回转换,请参阅Wikipedia.或者可能有一个API供您选择的编程语言.例如,Python有colorsys库.
我正在尝试在 Excel VBA 中制作一个基于 HSL 的色轮,使用微小的单元格作为“像素”,结果非常好,所以我想我会分享。
\n\n\n\n这演示了HSL 和 RGB之间的转换,以及如何以编程方式在任何网格 \xe2\x80\x94 甚至电子表格单元格上绘制线条/圆圈。
\n\n该代码已准备好按原样运行:
\n\nOption Explicit\n\nConst colorSheetName = "COLORS"\nConst pi = 3.14159265358979\nConst squareSize = 3.75 \'cell square size (pts)\nConst cDiameter = 80# \'circle diameter (cells)\nConst numAngles = 360# \'number of angles (lines to draw)\n\nSub CalculateColorWheel()\n Dim ws As Worksheet, radsPerAngle As Double, radius As Long, xStop As Double, _\n yStop As Double, z As Integer, xyLength As Double, lineDot As Long, _ \n lineLength As Long, h As Byte, s As Byte, v As Byte, r As Byte, g As Byte, b As Byte\n\n Set ws = ThisWorkbook.Sheets.Add \'create new worksheet\n On Error Resume Next \'ignore error \n Application.DisplayAlerts = False \'ignore warning\n ThisWorkbook.Sheets(colorSheetName).Delete \'delete worksheet (if exists)\n Application.DisplayAlerts = True \'stop ignoring warnings\n On Error GoTo 0 \'stop ignoring errors\n\n With ws\n .Name = colorSheetName \'name the new sheet\n .Rows.RowHeight = squareSize \'set rowheight \n .Columns.ColumnWidth=widthToColumnWidth(squareSize) \'match columnwidth to row\n ActiveWindow.DisplayGridlines = False \'hide gridlines\n ActiveWindow.DisplayHeadings = False \'hide row/col headings\n radius = cDiameter / 2 \'centre point\n lineLength = radius * 1.5 \'dots per angle (line)\n radsPerAngle = (360 / numAngles) * pi / 180 \'radians=a(degrees)\xc3\x97pi\xc3\xb7180\xc2\xb0\n\n Debug.Print "Grid size=" & .[a1].Height & "\xc3\x97" & .[a1].Width _\n & ", Diameter:" & cDiameter _\n & ", Area=" & Round(pi * radius ^ 2, 0) _\n & ", Circumference=" & Round(2 * pi * radius, 0) _\n & ", Radians per Angle=" & Round(radsPerAngle, 3) _\n & " \xc3\x97 " & numAngles & " angles" \'stats\n\n For z = 0 To numAngles - 1 \'loop through each angle\n For lineDot = 1 To lineLength \'loop thru length of line\n xyLength = radius * (lineDot / lineLength) \'calc dot xy& offset top-left\n xStop = Int(Cos(radsPerAngle * z) * xyLength) + radius + 2 \'x (column)\n yStop = Int(Sin(radsPerAngle * z) * xyLength) + radius + 2 \'y (row)\n\n If .Cells(yStop, xStop).Interior.Pattern=xlNone Then \'skip colored cells\n h = ((z + 1) / numAngles) * 255 \'hue=angle\n s = (lineDot / lineLength) * 255 \'saturation=radius\n v = 255 \'maximum brightness. (Adjustable)\n HSVtoRGB h, s, v, r, g, b \'convert HSV to RGB\n .Cells(yStop, xStop).Interior.Color=rgb(r,g,b) \'color the cell\n dots = dots + 1\n End If\n Next lineDot\n Application.StatusBar = Format(z / (numAngles - 1), "0%") \n DoEvents \'don\'t lag\n Next z\n End With\n Beep\n Application.StatusBar = "Finished drawing color circle (" & dots & " colors)"\nEnd Sub\n\nPublic Function widthToColumnWidth(pts As Double) As Double\n\'convert desired column width (points) to Excel "ColWidthUnits"\n\'12pts and under is a 1:12 ratio of (colWidthUnits:Pts).\n\' Over 12pts: 1:12 for 1st unit, then 1:(75/11) for remainder\n Select Case pts\n Case Is <= 0: widthToColumnWidth = 0\n Case Is <= 12: widthToColumnWidth = pts / 12\n Case Else: widthToColumnWidth = 1 + (pts - 12) / (75 / 11) \'\n End Select\nEnd Function\n\nPublic Sub HSVtoRGB(h As Byte, s As Byte, v As Byte, r As Byte, g As Byte, b As Byte)\n Dim minV As Byte, maxV As Byte, Chroma As Byte, tempH As Double\n\n If v = 0 Then\n r = 0: g = 0: b = 0\n Else\n If s = 0 Then\n r = v: g = v: b = v:\n Else\n maxV = v: Chroma = s / 255 * maxV: minV = maxV - Chroma\n Select Case h\n Case Is >= 170: tempH = (h - 170) / 43: g = 0\n If tempH < 1 Then\n b = maxV: r = maxV * tempH\n Else: r = maxV: b = maxV * (2 - tempH): End If\n Case Is >= 85: tempH = (h - 85) / 43: r = 0\n If tempH < 1 Then\n g = maxV: b = maxV * tempH\n Else: b = maxV: g = maxV * (2 - tempH): End If\n Case Else: tempH = h / 43: b = 0\n If tempH < 1 Then\n r = maxV: g = maxV * tempH\n Else: g = maxV: r = maxV * (2 - tempH): End If\n End Select\n r = r / maxV * (maxV - minV) + minV\n g = g / maxV * (maxV - minV) + minV\n b = b / maxV * (maxV - minV) + minV\n End If\n End If\nEnd Sub\nRun Code Online (Sandbox Code Playgroud)\n\n如何在 Excel 中运行此代码: 粘贴上面的代码并粘贴到常规模块中。(选择代码,Ctrl+C进行复制,然后在Excel中,按住Alt并点击F11++ ,然后点击+进行粘贴,然后I运行。)MCtrlVF5
\n\n| 归档时间: |
|
| 查看次数: |
16800 次 |
| 最近记录: |