获取填充颜色不受形状是否属于组的影响。获得正确形状的参考后,您就可以查看相应的单元格。
Visio 有两种主要的填充颜色设置方法 - 图案填充和渐变填充。后者是从 2013 年开始。
对于图案填充,您正在查看三个单元格:FillForegnd、FillBkgnd 和 FillPattern。大多数形状都以实心填充 (FillPattern 1) 开始,这意味着仅使用 FillForegnd。对于其他模式类型,您同时处理 FillForegnd 和 FillBkgnd。
对于渐变填充,FillGradientEnabled 单元格设置为 1,这会导致 Fill Gradient Stops 部分成为先例。
在后台,Visio 维护一个 Document.Colors 集合。一些内置颜色可以通过索引访问:0 = 黑色、1 = 白色、2 = 红色、3 = 绿色等,一直到 23。使用的任何其他自定义颜色都将添加到集合中,并且还会给出索引.这意味着,给定一个索引,您可以在 Colors 集合中查找颜色实例。
以下是一些代码,用于演示如何访问各种类型的着色。鉴于这四种形状:
前三个形状使用图案填充,最后一个使用渐变填充。
- Sheet.1 使用索引单元格公式 (
3),
- Sheet.2 使用 RGB 函数,
- Sheet.3 使用模式 (
2),因此同时使用前景和背景单元格
- Sheet.4 使用渐变色标,因此前景和背景单元格被忽略
...您可以使用以下代码读取工作中的颜色(注意这是使用 LINQPad 作为输出窗口,可以更清楚地看到发生了什么:
void Main()
{
var vApp = MyExtensions.GetRunningVisio();
for (int i = 1; i <= 4; i++)
{
var shp = vApp.ActivePage.Shapes.ItemFromID[i];
var colorInfos = new List<ColorInfo>();
colorInfos.Add(new ColorInfo(shp.CellsU["FillForegnd"]));
colorInfos.Add(new ColorInfo(shp.CellsU["FillBkgnd"]));
new
{
shp.NameID,
FillPattern = shp.CellsU["FillPattern"].ResultIU,
FillGradientEnabled = Convert.ToBoolean(shp.CellsU["FillGradientEnabled"].ResultIU),
PatternColors = colorInfos,
GradientColors = FillGradientColors(shp) ?? "Default (10 stops all white)"
}.Dump();
}
}
private dynamic FillGradientColors(Visio.Shape shp)
{
List<string> rgbs = null;
var iSect = (short)Visio.VisSectionIndices.visSectionFillGradientStops;
for (int i = 0; i < shp.RowCount[iSect]; i++)
{
var targetCell = shp.CellsSRC[iSect, (short)i, (short)Visio.VisCellIndices.visGradientStopColor];
if (targetCell.IsInherited == 0)
{
if (rgbs is null)
{
rgbs = new List<string>();
}
rgbs.Add(ColorInfo.RgbString(targetCell));
}
}
return rgbs;
}
public class ColorInfo
{
private Visio.Cell _vCell;
public ColorInfo(Visio.Cell vCell)
{
_vCell = vCell;
RGB = RgbString(_vCell);
}
public string Name => _vCell.Name;
public string RGB { get; set; }
public string FormulaU => _vCell.FormulaU;
public static string RgbString(Visio.Cell cell)
{
var colorIdx = cell.Result[(short)Visio.VisUnitCodes.visUnitsColor];
var c = cell.Document.Colors.Item16[(short)colorIdx];
return $"RGB({c.Red},{c.Green},{c.Blue})";
}
}
...这会产生以下输出: