In Looking up color table I showed how to look up colors from a growing table of color models, in order to create color swatches. These show distinct colors with a hard stop between them, like this
But lets say that we wanted the intermediate colors to be automatically calculated – creating a smooth transition between each color.
Creating Ramps to get between colors smoothly
You can calculate the transition between two colors by adjusting the red, blue and green mix proportional to the distance traveled between the source and target color. Putting it all together, you can see the smooth transition between each color in the swatch, compared to the discrete individual color components.
How does this work
This is covered in detail in the Color ramp library. Here’s the code for creating the diagram above, using the libraries in the cDataSet.xlsm workbook.
Public Sub makeRampedSwatches() Dim r As Range, nameList As Variant, i As Long, _ ramp As Variant, self As cDataSet, totalPoints As Long ' going to make some color swatches Set r = firstCell(wholeSheet("swatches")) r.Worksheet.Cells.clear r.Worksheet.Cells.Interior.color = vbWhite ' going to use some dulux names Const schemePrefix = "dulux-" Const width = 72 Const height = 64 Const pointsPerMilestone = 50 ' get the color map table With getcolorMap() ' refer to the dataset for later Set self = .self ' these are our colors nameList = Array( _ "charred chocolate", _ "charred clay", _ "cheater", _ "cheesy grin", _ "chenille" _ ) ' lets get the actual colors ReDim ramp(1 To arrayLength(nameList)) For i = LBound(nameList) To UBound(nameList) ramp(i + 1 - LBound(nameList)) = .value(schemePrefix & nameList(i), "rgb") Next i totalPoints = arrayLength(nameList) * pointsPerMilestone With r.Resize(2, totalPoints) .columnWidth = width / totalPoints .rowHeight = height End With ' ramp with intermediate colors For i = 1 To totalPoints With r.Offset(, i - 1) .Interior.color = rampLibraryRGB(ramp, 1, totalPoints, i) End With Next i ' add a title With r.Resize(1, 1) .value = "ramped swatch" .Font.color = makeColorProps(.Interior.color).textColor End With ' now contrast that with the non ramped For i = 1 To arrayLength(nameList) With r.Resize(1, pointsPerMilestone).Offset(1, (i - 1) * pointsPerMilestone) .Interior.color = ramp(i) With .Resize(1, 1) .Font.color = makeColorProps(.Interior.color).textColor .value = nameList(i + LBound(nameList) - 1) End With End With Next i 'cleanup .tearDown End With End Sub
For more color topics see here
For help and more information join our forum, follow the blog or follow me on Twitter.