vba 使用 CMYK 单元格值为 Excel 中的单元格着色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10690125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:13:07  来源:igfitidea点击:

Coloring cells in Excel with CMYK cell values

excelvbacolorsexcel-vbacmyk

提问by jeffrbauer

I'm looking for a way possibly using VBA to apply a cell's fill value by looking at specified fields in the same record. This would be a sample tab delimited Excel sheet:

我正在寻找一种可能使用 VBA 通过查看同一记录中的指定字段来应用单元格填充值的方法。这将是一个示例制表符分隔的 Excel 工作表:

BEGIN_DATA_FORMAT
SampleID    SAMPLE_NAME CMYK_C  CMYK_M  CMYK_Y  CMYK_K  LAB_L   LAB_A   LAB_B

BEGIN_DATA                              
1   1   100 0   0   60  34.16   -19.52  -27.46
2   2   100 100 0   60  22.02   6.27    -23.25
3   3   100 0   0   0   54.56   -31.12  -45.29
END_DATA

Fields 3-6 each contain the values for CMYK respectively. I'd like to apply a cell background fill to field 1 by parsing each record for the combined CMYK values as a starting point.

字段 3-6 分别包含 CMYK 的值。我想通过解析组合 CMYK 值的每个记录作为起点,将单元格背景填充应用于字段 1。

Conversion to RGB or HSL may need to be done initially unless there's a backdoor method to set CMYK values in the Excel/Windows color picker.

除非有后门方法可以在 Excel/Windows 颜色选择器中设置 CMYK 值,否则最初可能需要转换为 RGB 或 HSL。

采纳答案by SeanC

this will give you the RGB from the CYMK in your data:

这将为您提供来自数据中 CYMK 的 RGB:

Function CYMK2RGB(c As Integer, y As Integer, m As Integer, k As Integer) As Long
Dim R As Integer
Dim G As Integer
Dim B As Integer
Dim colors As Integer

colors = 255 * (100 - k) / 100
R = colors * (100 - c) / 100
G = colors * (100 - m) / 100
B = colors * (100 - y) / 100

CYMK2RGB = RGB(R, G, B)
End Function

using range("A1").Interior.Color=cymk2rgb(...)will set the color - note it's not going to be an exact match, as CYMK is subtractive, and RGB is additive. this site: http://www.printernational.org/rgb-versus-cmyk.phphas more details comparing the two.

usingrange("A1").Interior.Color=cymk2rgb(...)将设置颜色 - 请注意,它不会完全匹配,因为 CYMK 是减法,而 RGB 是加法。这个网站:http: //www.printernational.org/rgb-versus-cmyk.php有更多比较两者的细节。