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

时间:2015-07-30 09:02:16

标签: c# colors layer visio

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

我需要一种方法来改变" 19"到RGB方案,取决于当前的方案和变体。

的Heiko

1 个答案:

答案 0 :(得分:2)

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)
        );
}