visio c# 从图层中获取 RGB 颜色

visio c# get RGB-Color from a layer

我尝试将 visio 文档中图层的当前颜色获取为 RGB。我的问题是颜色,在公式中没有用 "RGB(1,2,3)" 设置。根据当前方案,设置了一些颜色。所以有“255”(未选择图层颜色)或“19”(使用的颜色取决于活动方案,例如深灰色)的颜色。

我需要一种方法将“19”转换为 RGB 方案,具体取决于当前方案和变体。

黑子

Visio 已修复前 24 种颜色。所有其他的都以 RGB(R, G, B) 公式的形式出现。固定颜色列表可以从 Document.Colors 中获得。总而言之,您可以从以下开始:

using System.Drawing;
using System.Text.RegularExpressions;
using Visio = Microsoft.Office.Interop.Visio;

static Color GetLayerColor(Visio.Layer layer)
{
    var str = layer
        .CellsC[(short)Visio.VisCellIndices.visLayerColor]
        .ResultStrU[""];

    // case 1: fixed color
    int colorNum;
    if (int.TryParse(str, out colorNum))
    {
        var visioColor = layer.Document.Colors[colorNum];

        return Color.FromArgb(
            visioColor.Red, 
            visioColor.Green, 
            visioColor.Blue);
    }

    // case 2: RGB formula
    var m = Regex.Match(str, @"RGB\((\d+),\s*(\d+),\s*(\d+)\)").Groups;

    return Color.FromArgb(
        int.Parse(m[1].Value), 
        int.Parse(m[2].Value), 
        int.Parse(m[3].Value)
        );
}