本文介绍了Interior.Color属性反转颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一段代码,让我能够检索excel工作簿中的特定单元格的阴影颜色。我已经通过使用MATLAB的



然后我运行我的MATLAB代码,它提取B列的每个单元格的颜色信息。我应该得到一个具有相同垂直顺序的颜色的图形,以及每个颜色的int值和RGB三元组的表。



!看看结果:





请注意,从颜色属性获得的整数值并不总是与原始单元格的颜色匹配,对于黑色,白色,绿色和洋红色,整数值是正确的,但是对于所有其他颜色都不是这样。例如,您可以看到,对于Excel上的红色,输出int和RGB三元组对应于蓝色。



我已经添加了正确的结果我应该得到,供参考:

 颜色Int RGB 
-------- --- ----- -----
黑色0 0 0 0
白色16777215 1 1 1
红色16711680 1 0 0
绿色65280 0 1 0
蓝色255 0 0 1
青色65535 0 1 1
洋红16711935 1 0 1
黄色16776960 1 1 0

我使用。



如果我们比较两个表,我们可以推断出红色和蓝色通道是反转。 / p>

代码:



我执行运行测试的方法称为 getCellColor 。看看代码:

  function getCellColor()
清除所有;
clc;

%Excel
filename ='colorTest.xlsx';

%在本地主机上启动Excel作为ActiveX服务器进程
Excel = actxserver('Excel.Application');

%处理请求的Excel工作簿文件名
path = validpath(filename);

%功能完成后的清理任务
cleanUp = onCleanup(@()xlsCleanup(Excel,path));

%打开Excel工作簿。
readOnly = true;
[〜,workbookHandle] = openExcelWorkbook(Excel,path,readOnly);

%初始化工作表对象
workSheets = workbookHandle.Worksheets;

%获取工作表对象(工作表#1)
sheet = get(workSheets,'item',1);

%打印表头
fprintf('Color \t Int \t R G B\\\
');
fprintf('-------- \t -------- \t ----- \\\
');

%创建图
数字;
持有;

%循环遍历Excel文件中的每个颜色
for row = 1:8
%获取颜色名称为
的单元对象cell = get(sheet, '细胞',行,1);
cName = cell.value;

%获取具有彩色背景的单元格对象
cell = get(sheet,'Cells',row,2);

%获取内部对象
interior = cell.Interior;

%获取颜色整数属性
cInt = get(interior,'Color'); %< - 在此特别注意(*)

%从整数值获取RGB三元组
cRGB = int2rgb(cInt);

%绘制颜色
patch([0 0 1 1],[8行9行9行8行],cRGB);

%打印带颜色数据的行
fprintf('% - 8s\t%8d\t%d%d%d\\\
',cName,cInt,cRGB);
end

%关闭轴
set(findobj(gcf,'type','axes'),'Visible','off')

end

(*)此指令负责恢复颜色整数。






注意:下面介绍的功能不会导致问题,因为它们不参与获取颜色整数(它们仅用于辅助任务)。我已经将此信息仅包括在内。



在此过程中,我使用了MATLAB的 iofun 中的三个私有函数文件夹,它们是: validpath xlsCleanup openExcelWorkbook 。我简单地将它们复制到项目文件夹内的一个名为 private 的文件夹。



最后,要从颜色整数中获取RGB三元组,我使用我从修改的功能我在网上找到。



以下是我的代码:code> int2rgb function:

  function [RGB] = int2rgb(colorInt)
%返回RGB整数的RGB三元组。

如果colorInt> 16777215 || colorInt 0
错误('intvalid int value。有效范围:0 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






我正在努力有一些感觉,但我真的不知道发生了什么。我做了一些研究,没有多少运气,但和引起了我的注意。也许这与我的问题有关。



Interior.Color属性是否真的反转颜色?



如果是这种情况,我应该认为这是正常行为还是这个错误?






链接到



我已将整个项目打包在一个 .zip 文件中,并将其上传,所以你可以直接在你的机器上运行这个测试。下载文件并打开包装。



解决方案

正如在,在颜色编码方面没有正确或错误。微软选择了这种特殊的方法来编码颜色,只是他们知道的原因,我应该坚持下去。



所以我得到的RGB值是完全正确的。 p>

在下表中,我将MSDN文章中提供的RGB值包含在我在MATLAB中获得的RGB值,它们是完美匹配的。

 来自MSDN的颜色Int RGB值
-------- -------- ---- ----
黑色0 0
白色16777215 16777215
红色255 255
绿色65280 65280
蓝色16711680 16711680
青色16776960 16776960
洋红16711935 16711935
黄色65535 65535


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:

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:

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 B\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 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

解决方案

From the MSDN article on RGB Color Model:

As it was suggested in chris neilsen answer, there is no "right" or "wrong" in terms of color encoding. Microsoft has chosen this particular way to encode colors for reasons only they know, and I should stick to it.

So the RGB values that I get are totally correct.

In the following table, I have included the RGB values provided in the MSDN article next to the ones that I get in MATLAB, and they are a perfect match.

Color        Int         RGB values from MSDN
--------     --------    --------
Black               0           0
White        16777215    16777215
Red               255         255
Green           65280       65280
Blue         16711680    16711680
Cyan         16776960    16776960
Magenta      16711935    16711935
Yellow          65535       65535

这篇关于Interior.Color属性反转颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:30