Interior.Color Property inverts colors?

calendar_today Asked Feb 6, 2016
thumb_up 5 upvotes
history Updated April 14, 2026

Direct Answer

There is no "right" or "wrong" here, Matlab and Excel just encode color differently. You need to account for that in your code. The closest I can find to an official source is…. This is an advisory response with reference links, ranked #303rd of 303 by community upvote score, from 2016.


The Problem (Q-score 5, ranked #303rd of 303 in the Excel VBA archive)

The scenario as originally posted in 2016

I have written a piece of code that allows me to retrieve the shading color of a specific cell inside a sheet of an excel workbook. I have successfully retrieved the RGB integer value by starting a COM server using MATLAB’s actxserver, and then accessing the Color Property of the Interior Object of that particular Cell Object. Then I obtain the equivalent RGB triplet of that integer, so I can use it later for plotting in MATLAB.

In order to test that my code works properly I designed the following test: I created an Excel workbook called colorTest.xlsx with 8 different colors:

enter image description here

Then I run my MATLAB code, which extracts the color information on each cell of the B column. I should get a plot with the colors on the same vertical order and a table with the int value and the RGB triplet of each color.

However something unexpected happens! Look at the results:

enter image description here

Notice that the integer value that is obtained from the Color Property does not always match the color of the original cell, for black, white, green and magenta the integer values are correct, but this is not true for all the other colors. You can see, for example, that for red color on the Excel, the output int and RGB triplet correspond to blue color.

I have added the following table with the correct results I should get, for reference:

Color        Int         R G B
--------     --------    -----
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 obtained the correct integer values for each color using this RGB Int Calculator.

If we compare the two tables, we can deduce that the Red and Blue channels are inverted.

The code:

The function that I execute to run the test is called getCellColor. Have 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 R G Bn');
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('%-8st %8dt %d %d %dn', cName, cInt, cRGB);
end

% Turn off axes
set(findobj(gcf, 'type','axes'), 'Visible','off')

end

(*) This instruction is responsible of recovering the color integer.


Note: The functions described next, do not cause the problem since they do not take part in the obtaining of the color integer (they are only used for secondary tasks). I have included this information only for completeness.

During this process I use three private functions from the MATLAB’s iofun folder, which are: validpath, xlsCleanup and openExcelWorkbook. I simply copied them into a folder called private inside the project folder.

Finally, to obtain the RGB triplet from the color integer, I use a function which I adapted from this other function that I found on the net.

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 make some sense out of this, but I really have no idea of what is happening. I have done some research, without much luck, but this post and this other post caught my attention. Maybe it has something to do with my problem.

So does the Interior.Color Property really inverts colors?

If this is the case, should I consider this as normal behavior or is this a bug?


Link to download:

I have packed the entire project on a .zip file and uploaded it, so you can run this test on your machine straight away. Download the file and unpack.

getCellColor.zip

Why community consensus is tight on this one

Across 303 Excel VBA entries in the archive, the accepted answer here holds niche answer (below median) status — meaning voters are unusually aligned on the right fix.


The Verified Solution — niche answer (below median) (+5)

Advisory answer — community consensus with reference links

Note: the verified answer below is a reference / advisory response rather than a copy-ready snippet.

There is no “right” or “wrong” here, Matlab and Excel just encode color differently. You need to account for that in your code.

The closest I can find to an official source is this MSDN article, about half way down see the example of encoding of “blue”

MSDN article

The following examples set the interior of a selection of cells to the color blue.
Selection.Interior.Color = 16711680
Selection.Interior.Color = &HFF0000
Selection.Interior.Color = &O77600000
Selection.Interior.Color = RGB(0, 0, 255)


When to Use It — classic (2013–2016)

Ranked #303rd in its category — specialized fit

This pattern sits in the 99% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Excel VBA archive for a higher-consensus alternative.

What changed between 2016 and 2026

The answer is 10 years old. The Excel VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

This is a below-median answer — when does it still fit?
expand_more

Answer score +5 vs the Excel VBA archive median ~4; this entry is niche. The score plus 5 supporting upvotes on the question itself (+5) means the asker and 4 subsequent voters all validated the approach.

This answer links out — what are the reference links worth following?
expand_more

Read the first external link for the canonical reference, then search this archive for a top-10 entry in the same category — advisory answers are best paired with a ranked code snippet to close the loop.

Published around 2016 — what’s changed since?
expand_more

Published 2016, which is 10 year(s) before today’s Office 2026 build. The Excel VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Excel VBA pattern ranks just above this one at #302?
expand_more

The pattern one rank above is “Open a workbook from VBA and disable Workbook_Open() code?”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 5, Answer-score 5, original post 2016, ranked #303rd of 303 in the Excel VBA archive. Last regenerated April 14, 2026.