API for comparing colors
I continue to find the topic of comparing colors a fascinating one. As an experiment to see if I could create a REST API serving up JSON and JSONP from a Google Apps Script content service app, I wanted to see if I could create something that, given a color code, would find the nearest ‘n’ matching colors in some defined color scheme, such as pantone, dulux, htm colors and so on.
Just for fun, I also implemented the color database in both parse.com and scriptb.
A VBA consumer
Now all that is working, I needed to create something to act as a client. I have already implemented color matching natively in both VBA and Google Apps Script, but now I needed to create something that would delegate the database management and color matchs to a GAS web app and just query and display the results.
First off, here is the VBA version. Here’s the first 5 closest matching colors to some random hex codes from the pantone fashion and home library. I think the algorithm does a pretty good job – and all the processing is delegated off to the GAS modules described here.
For simplicity, Im using the rest/excel library to manage all the jSon stuff and web interaction details. That means that the final client procedure is rather trivial as below.
Public Sub testColorSchemerClosest()
Dim scheme As String, joc As cJobject
Dim job As cJobject, jt As cJobject, n As Long
scheme = InputBox("Which scheme (htm,pms,pfh or dulux)?")
n = 0
With restQuery("colorschemer", "colorschemer", , _
"target", , , , False, , , , , , , , , _
"&provider=parse&closest=5&scheme=" & scheme)
For Each job In .jObjects
n = n + 1
For Each joc In job.child("c").children
With .dset.headingRow.where.Offset(n, joc.childIndex).Resize(1, 1)
.Interior.Color = joc.child("properties.rgb").value
Set jt = joc.child("colortable.label")
' would only exist if this was from a scheme
If (jt Is Nothing) Then
.value = joc.child("colortable.hex").value
.value = jt.value
.Font.Color = joc.child("properties.textColor").value
' modify heading
.dset.headingRow.headings(2).where.value = "Matching this against scheme " & scheme
The only problem is that it is pretty slow. See here for a look at GAS performance. with a focus on lots of math. Using Parse.com or scriptdb as the underlying database is not a big factor. I’ll publish some comparison stats on that later.
More color schemes
I’m looking for more color schemes. If you would like to contribute a color scheme, all i need is the link to a Google Document with the scheme in it, like the colortable tab in this spreadsheet.
You can download the VBA code and libraries mentioned here in the cDataSet.xlsm workbook – the colorschemer tab, from Excel liberation.