【问题标题】:visio c# get RGB-Color from a layervisio c# 从图层中获取 RGB-Color
【发布时间】:2015-07-30 09:02:15
【问题描述】:

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

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

海子

【问题讨论】:

  • 这对你不起作用吗?颜色 c = Color.FromArgb(你的整数值);这将为您提供 ARGB 格式的颜色,您可以轻松提取 R G 和 B 的值

标签: c# colors layer visio


【解决方案1】:

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

【讨论】:

  • 非常感谢。这只需稍作改动即可。如果没有检查和选择图层颜色,则 colorNum = 255 并且 layer.Document.Colors[colorNum] 中没有响应颜色。发生这种情况时,我返回 Color.empty。
猜你喜欢
  • 2017-03-30
  • 2013-07-10
  • 1970-01-01
  • 2011-06-12
  • 2011-04-18
  • 2013-10-13
  • 2014-12-12
  • 2014-12-25
相关资源
最近更新 更多