- Excel Version
- 2016
There are simplistic equations to add the RGB components of two colours, but they yield deficient results.
A while ago a JavaScript library was released under the Creative Commons license, allowing to realistic pigment mixing. Here we will see how to run the JS code from Excel:
A while ago a JavaScript library was released under the Creative Commons license, allowing to realistic pigment mixing. Here we will see how to run the JS code from Excel:
- Create a local HTML file by typing the code below and saving it with a HTM extension; this page will serve as interface between Excel and JS.
- Note that Selenium must be installed in order to make the VBA code work.
- Input the RGB values as shown in the picture; the code will send them to the HTML, which in turn will call the JS to perform the calculations.
- A separate local JS snippet clears the input boxes as the VBA loops the rows. We can have JS embedded in HTML, locally stored or dwelling on a Web page.
- The resulting RGB components are written to the HTML file; VBA fetches that, transferring it back to the spreadsheet.
- This version retrieves the main JS from a URL, but it will probably also work if the lengthy routine is residing on your hard drive.
VBA Code:
Dim bot As New ChromeDriver ' Selenium
Sub colours()
Dim col1$, col2$, box1 As WebElement, cr%, i%, v
bot.Start "Chrome"
bot.Get "c:\temp\colors.htm" ' local page
cr = 11
For i = 4 To 8 ' the rows
col1 = "rgb(" & Cells(i, 3) & ", " & Cells(i, 4) & ", " & Cells(i, 5) & ")"
col2 = "rgb(" & Cells(i, 6) & ", " & Cells(i, 7) & ", " & Cells(i, 8) & ")"
Application.Wait Now + TimeValue("0:00:03")
Set box1 = bot.FindElementById("first")
Application.Wait Now + TimeValue("0:00:03")
box1.SendKeys (col1)
bot.FindElementById("sec").SendKeys (col2)
bot.FindElementById("chocol").Click
v = Split(bot.FindElementById("name").Text, ",") ' resulting color
Cells(i, 10) = Split(v(0), "(")(1)
Cells(i, 11) = v(1)
Cells(i, 12) = Split(v(2), ")")(0)
Cells(cr, 3).Interior.Color = RGB(Cells(i, 3), Cells(i, 4), Cells(i, 5))
Cells(cr, 7).Interior.Color = RGB(Cells(i, 6), Cells(i, 7), Cells(i, 8))
Cells(cr, 10).Interior.Color = RGB(Cells(i, 10), Cells(i, 11), Cells(i, 12))
cr = cr + 3
bot.FindElementById("btn").Click ' clear fields
Next
End Sub
Rich (BB code):
const bton = document.getElementById('btn');
bton.addEventListener('click', function handleClick(event) {
event.preventDefault();
const firstval = document.getElementById('first');
const secval=document.getElementById('sec');
firstval.value = '';
secval.value= '';
});
// I am eraser.js