I wrote a code snippet that allows me to get the shading color of a specific cell inside an Excel workbook sheet. I successfully got the integer RGB value by starting the COM server using MATLAB actxserver , and then accessed the Color Property of the internal object of this particular cell object. Then I get the equivalent RGB triplet of this integer, so I can use it later for building in MATLAB.
To test the performance of my code, I developed the following test: I created an Excel workbook called colorTest.xlsx with 8 different colors:

Then I run my MATLAB code, which retrieves the color information in each cell of column B. I should get a plot with colors in the same vertical order and a table with an int value and an RGB triplet of each color.
However, something unexpected happens! Look at the results:

Please note that the integer value obtained from the color property does not always correspond to the color of the original cell, for black, white, green and magenta values the integer values are correct, but this is not true for all other colors, For example, you can see that for red in Excel, the output tap and RGB trigger are blue.
I have added the following table with the correct results I should get for reference:
Color Int RGB -------- -------- ----- Black 0 0 0 0 White 16777215 1 1 1 Red 16711680 1 0 0 Green 65280 0 1 0 Blue 255 0 0 1 Cyan 65535 0 1 1 Magenta 16711935 1 0 1 Yellow 16776960 1 1 0
I got the correct integer values for each color using this RGB Int calculator. .
If we compare the two tables, we can conclude that the Red and Blue channels are inverted .
Code:
The function that I run to run the test is called getCellColor . Take a look at the code:
function getCellColor() clear all; clc; % Excel filename = 'colorTest.xlsx'; % Start Excel as ActiveX server process on local host Excel = actxserver('Excel.Application'); % Handle requested Excel workbook filename path = validpath(filename); % Cleanup tasks upon function completion cleanUp = onCleanup(@()xlsCleanup(Excel, path)); % Open Excel workbook. readOnly = true; [~, workbookHandle] = openExcelWorkbook (Excel, path, readOnly); % Initialise worksheets object workSheets = workbookHandle.Worksheets; % Get the sheet object (sheet #1) sheet = get(workSheets,'item',1); % Print table headers fprintf('Color \t Int \t RGB\n'); fprintf('--------\t --------\t -----\n'); % Create figure figure; hold on; % Loop through every color on the Excel file for row = 1:8 % Get the cell object with name of color cell = get(sheet, 'Cells', row, 1); cName = cell.value; % Get the cell object with colored background cell = get(sheet, 'Cells', row, 2); % Get the interior object interior = cell.Interior; % Get the color integer property cInt = get(interior, 'Color'); % <-- Pay special attention here(*) % Get the RGB triplet from its integer value cRGB = int2rgb(cInt); % Plot the color patch([0 0 1 1], [8-row 9-row 9-row 8-row], cRGB); % Print row with color data fprintf('%-8s\t %8d\t %d %d %d\n', cName, cInt, cRGB); end % Turn off axes set(findobj(gcf, 'type','axes'), 'Visible','off') end
(*) This instruction is responsible for restoring an integer color.
Note: The functions described below do not cause problems because they are not involved in obtaining the whole color (they are used only for secondary tasks). I have included this information for completeness only.
During this process, I use three private functions from the MATLAB iofun folder: validpath , xlsCleanup and openExcelWorkbook . I just copied them to a folder called private inside the project folder.
Finally, to get the RGB triplet from an integer number of colors, I use a function that I adapted from this other function that I found on the network.
Here is the code for my int2rgb function:
function[RGB] = int2rgb(colorInt) % Returns RGB triplet of an RGB integer. if colorInt > 16777215 || colorInt < 0 error ('Invalid int value. Valid range: 0 <= value <= 16777215') end R = floor(colorInt / (256*256)); G = floor((colorInt - R*256*256)/256); B = colorInt - R*256*256 - G*256; RGB = [R, G, B]/255; end
I am trying to figure this out, but I really don't know what is going on. I did some research without much luck, but this post and this other post caught my attention. Perhaps this has something to do with my problem.
So does the Interior.Color property really invert colors?
If so, should this be considered normal behavior or is it a mistake?
Download link:
I packed the entire project into a .zip file and downloaded it, so you can immediately run this test on your machine. Download the file and unzip it.
getCellColor.zip