开发者

How to calculate the color based on given fgColor indexed

开发者 https://www.devze.com 2023-03-20 08:51 出处:网络
I am working on Open XML, <x:fill> <x:patternFill patternType=\"solid\"> <x:fgColor indexed=\"46\" />

I am working on Open XML,

<x:fill>
    <x:patternFill patternType="solid">
      <x:fgColor indexed="46" />
      <x:bgColor indexed="64" />
    </x:patternFill>
  </x:fill>

Above is a office 2007 document that converted from office 2003. According to http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.foregroundcolor.aspx Indexed attributes only used for backward compatibility purposes.

Above is my code, how to calculate the开发者_Go百科 #Hex Color Code for indexed = 46?


I made this class for myself. Hope I save someone some trouble

public class IndexedColours
    {
        static Dictionary<string, string> Data;
        IndexedColours()
        {
            if(Data == null || Data.Count == 0)
            {
                Data = new Dictionary<string, string>();
                Data.Add("0", "000000");
                Data.Add("1", "FFFFFF");
                Data.Add("2", "FF0000");
                Data.Add("3", "00FF00");
                Data.Add("4", "0000FF");
                Data.Add("5", "FFFF00");
                Data.Add("6", "FF00FF");
                Data.Add("7", "00FFFF");
                Data.Add("8", "000000");
                Data.Add("9", "FFFFFF");
                Data.Add("10", "FF0000");
                Data.Add("11", "00FF00");
                Data.Add("12", "0000FF");
                Data.Add("13", "FFFF00");
                Data.Add("14", "FF00FF");
                Data.Add("15", "00FFFF");
                Data.Add("16", "800000");
                Data.Add("17", "008000");
                Data.Add("18", "000080");
                Data.Add("19", "808000");
                Data.Add("20", "800080");
                Data.Add("21", "008080");
                Data.Add("22", "C0C0C0");
                Data.Add("23", "808080");
                Data.Add("24", "9999FF");
                Data.Add("25", "993366");
                Data.Add("26", "FFFFCC");
                Data.Add("27", "CCFFFF");
                Data.Add("28", "660066");
                Data.Add("29", "FF8080");
                Data.Add("30", "0066CC");
                Data.Add("31", "CCCCFF");
                Data.Add("32", "000080");
                Data.Add("33", "FF00FF");
                Data.Add("34", "FFFF00");
                Data.Add("35", "00FFFF");
                Data.Add("36", "800080");
                Data.Add("37", "800000");
                Data.Add("38", "008080");
                Data.Add("39", "0000FF");
                Data.Add("40", "00CCFF");
                Data.Add("41", "CCFFFF");
                Data.Add("42", "CCFFCC");
                Data.Add("43", "FFFF99");
                Data.Add("44", "99CCFF");
                Data.Add("45", "FF99CC");
                Data.Add("46", "CC99FF");
                Data.Add("47", "FFCC99");
                Data.Add("48", "3366FF");
                Data.Add("49", "33CCCC");
                Data.Add("50", "99CC00");
                Data.Add("51", "FFCC00");
                Data.Add("52", "FF9900");
                Data.Add("53", "FF6600");
                Data.Add("54", "666699");
                Data.Add("55", "969696");
                Data.Add("56", "003366");
                Data.Add("57", "339966");
                Data.Add("58", "003300");
                Data.Add("59", "333300");
                Data.Add("60", "993300");
                Data.Add("61", "993366");
                Data.Add("62", "333399");
                Data.Add("63", "333333");
            }
        }

        public static string GetIndexColour(string Index)
        {
            var d = new IndexedColours();
            var res = "";
            var exist = Data.TryGetValue(Index, out res);
            if (exist)
                return res;
            else return "000000";
        }
    }

Method to use the above code:

public static string GetCellColour(this Cell cell, SpreadsheetDocument d)
        {
            if (cell != null && cell.StyleIndex != null)
            {
                var valcell = cell;

                var styles = d.WorkbookPart.WorkbookStylesPart;
                var ss = styles.Stylesheet;
                var formats = ss.CellFormats;

                var cf = (CellFormat)formats.ElementAt((int)valcell.StyleIndex.Value);
                var fill = (Fill)styles.Stylesheet.Fills.ChildElements[(int)cf.FillId.Value];
                var fgc = fill.PatternFill.ForegroundColor;
                var cl = fgc == null || fgc.Rgb == null ? "FFFFFF" : fgc.Rgb.Value.Remove(0, 2);
                if (fgc != null && fgc.Indexed != null && fgc.Indexed.HasValue)
                {
                    cl = IndexedColours.GetIndexColour(fgc.Indexed.Value.ToString());
                }
                return cl;
            }
            else return "FFFFFF";
        }


Solved my problem, there is color indexes online. just search for it.

0

精彩评论

暂无评论...
验证码 换一张
取 消