• If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password.
  • Excel articles and downloadable files provided in the articles have not been reviewed by MrExcel Publishing. Please apply the provided methods / codes and open the files at your own risk. If you have any questions regarding an article, please use the Article Discussion section.
Worf

Using Excel to mix colours

Excel Version
  1. 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:

  • 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.
result.png


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

html_colours.png


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
Author
Worf
Views
1,859
First release
Last update

Ratings

0.00 star(s) 0 ratings

More Excel articles from Worf

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top