示例#1
0
        private byte[] GetRGBOrARGB()
        {
            byte[] rgb = null;

            if (ctColor.indexedSpecified && ctColor.indexed > 0)
            {
                HSSFColor indexed = (HSSFColor)HSSFColor.GetIndexHash()[(int)ctColor.indexed];
                if (indexed != null)
                {
                    rgb    = new byte[3];
                    rgb[0] = (byte)indexed.GetTriplet()[0];
                    rgb[1] = (byte)indexed.GetTriplet()[1];
                    rgb[2] = (byte)indexed.GetTriplet()[2];
                    return(rgb);
                }
            }

            if (!ctColor.IsSetRgb())
            {
                // No colour is available, sorry
                return(null);
            }

            // Grab the colour
            rgb = ctColor.GetRgb();

            // Correct it as needed, and return
            return(rgb);
        }
示例#2
0
 public void Compare(HSSFColor expected, HSSFColor palette)
 {
     short[] s1 = expected.GetTriplet();
     short[] s2 = palette.GetTriplet();
     Assert.AreEqual(s1[0], s2[0]);
     Assert.AreEqual(s1[1], s2[1]);
     Assert.AreEqual(s1[2], s2[2]);
 }
示例#3
0
 private byte[] GetRGBOrARGB()
 {
     if (this.ctColor.indexedSpecified && this.ctColor.indexed > 0U)
     {
         HSSFColor hssfColor = (HSSFColor)HSSFColor.GetIndexHash()[(object)(int)this.ctColor.indexed];
         if (hssfColor != null)
         {
             return new byte[3] {
                        (byte)hssfColor.GetTriplet()[0], (byte)hssfColor.GetTriplet()[1], (byte)hssfColor.GetTriplet()[2]
             }
         }
         ;
     }
     if (!this.ctColor.IsSetRgb())
     {
         return((byte[])null);
     }
     return(this.CorrectRGB(this.ctColor.GetRgb()));
 }
示例#4
0
        public void TestCustomPalette()
        {
            //reading sample xls
            HSSFWorkbook book = HSSFTestDataSamples.OpenSampleWorkbook("Simple.xls");

            //creating custom palette
            HSSFPalette palette = book.GetCustomPalette();

            palette.SetColorAtIndex((short)0x12, (byte)101, (byte)230, (byte)100);
            palette.SetColorAtIndex((short)0x3b, (byte)0, (byte)255, (byte)52);

            //writing to disk; reading in and verifying palette
            string     tmppath = TempFile.GetTempFilePath("TestCustomPalette", ".xls");
            FileStream fos     = new FileStream(tmppath, FileMode.OpenOrCreate);

            book.Write(fos);
            fos.Close();

            FileStream fis = new FileStream(tmppath, FileMode.Open, FileAccess.Read);

            book = new HSSFWorkbook(fis);
            fis.Close();

            palette = book.GetCustomPalette();
            HSSFColor color = palette.GetColor(HSSFColor.CORAL.index);  //unmodified

            Assert.IsNotNull(color, "Unexpected null in custom palette (unmodified index)");
            short[] expectedRGB = HSSFColor.CORAL.triplet;
            short[] actualRGB   = color.GetTriplet();
            String  msg         = "Expected palette position to remain unmodified";

            Assert.AreEqual(expectedRGB[0], actualRGB[0], msg);
            Assert.AreEqual(expectedRGB[1], actualRGB[1], msg);
            Assert.AreEqual(expectedRGB[2], actualRGB[2], msg);

            color = palette.GetColor((short)0x12);
            Assert.IsNotNull(color, "Unexpected null in custom palette (modified)");
            actualRGB = color.GetTriplet();
            msg       = "Expected palette modification to be preserved across save";
            Assert.AreEqual((short)101, actualRGB[0], msg);
            Assert.AreEqual((short)230, actualRGB[1], msg);
            Assert.AreEqual((short)100, actualRGB[2], msg);
        }
示例#5
0
        protected byte[] GetRGBOrARGB()
        {
            if (IsIndexed && Index > 0)
            {
                int       indexNum = Index;
                HSSFColor indexed  = HSSFColor.GetAllColors()[indexNum];
                if (indexed != null)
                {
                    byte[] rgb = new byte[3];
                    rgb[0] = (byte)indexed.GetTriplet()[0];
                    rgb[1] = (byte)indexed.GetTriplet()[1];
                    rgb[2] = (byte)indexed.GetTriplet()[2];
                    return(rgb);
                }
            }

            // Grab the colour
            return(StoredRBG);
        }
        public static byte[] GetDefaultRGB(int index)
        {
            HSSFColor hssfColor = HSSFColor.GetIndexHash()[index];

            if (hssfColor == null)
            {
                return(null);
            }
            byte[] rgbShort = hssfColor.GetTriplet();
            return(rgbShort);
        }
示例#7
0
        protected byte[] GetRGBOrARGB()
        {
            if (IsIndexed && Index > 0)
            {
                int       indexNum  = Index;
                var       hashIndex = HSSFColor.GetIndexHash();
                HSSFColor indexed   = null;
                if (hashIndex.ContainsKey(indexNum))
                {
                    indexed = hashIndex[indexNum];
                }
                if (indexed != null)
                {
                    byte[] rgb = new byte[3];
                    rgb[0] = (byte)indexed.GetTriplet()[0];
                    rgb[1] = (byte)indexed.GetTriplet()[1];
                    rgb[2] = (byte)indexed.GetTriplet()[2];
                    return(rgb);
                }
            }

            // Grab the colour
            return(StoredRBG);
        }
示例#8
0
        public void TestDefaultPalette()
        {
            PaletteRecord palette = new PaletteRecord();

            //make sure all the HSSFColor constants match
            Dictionary <int, HSSFColor> colors = HSSFColor.GetIndexHash();

            foreach (KeyValuePair <int, HSSFColor> entry in colors)
            {
                int       index          = entry.Key;
                HSSFColor c              = entry.Value;
                byte[]    rgbTriplet     = c.GetTriplet();
                byte[]    paletteTriplet = palette.GetColor((short)index);
                String    msg            = "Expected HSSFColor constant to match PaletteRecord at index 0x"
                                           + NPOI.Util.StringUtil.ToHexString(c.Indexed);
                Assert.AreEqual(rgbTriplet[0], paletteTriplet[0] & 0xff, msg);
                Assert.AreEqual(rgbTriplet[1], paletteTriplet[1] & 0xff, msg);
                Assert.AreEqual(rgbTriplet[2], paletteTriplet[2] & 0xff, msg);
            }
        }
示例#9
0
        public void TestDefaultPalette()
        {
            PaletteRecord palette = new PaletteRecord();

            //make sure all the HSSFColor constants match
            Hashtable   colors  = HSSFColor.GetIndexHash();
            IEnumerator indexes = colors.Keys.GetEnumerator();

            while (indexes.MoveNext())
            {
                int       index          = (int)indexes.Current;
                HSSFColor c              = (HSSFColor)colors[index];
                short[]   rgbTriplet     = c.GetTriplet();
                byte[]    paletteTriplet = palette.GetColor((short)index);
                String    msg            = "Expected HSSFColor constant to match PaletteRecord at index 0x"
                                           + NPOI.Util.StringUtil.ToHexString(c.GetIndex());
                Assert.AreEqual(rgbTriplet[0], paletteTriplet[0] & 0xff, msg);
                Assert.AreEqual(rgbTriplet[1], paletteTriplet[1] & 0xff, msg);
                Assert.AreEqual(rgbTriplet[2], paletteTriplet[2] & 0xff, msg);
            }
        }
示例#10
0
        public void Test48403()
        {
            HSSFWorkbook wb = new HSSFWorkbook();

            var         color   = new Rgb24(0, 0x6B, 0x6B); //decode("#006B6B");
            HSSFPalette palette = wb.GetCustomPalette();

            HSSFColor hssfColor = palette.FindColor(color.R, color.G, color.B);

            Assert.IsNull(hssfColor);

            palette.SetColorAtIndex(
                (short)(PaletteRecord.STANDARD_PALETTE_SIZE - 1),
                (byte)color.R, (byte)color.G,
                (byte)color.B);
            hssfColor = palette.GetColor((short)(PaletteRecord.STANDARD_PALETTE_SIZE - 1));
            Assert.IsNotNull(hssfColor);
            Assert.AreEqual(55, hssfColor.Indexed);
            CollectionAssert.AreEqual(new short[] { 0, 107, 107 }, hssfColor.GetTriplet());

            wb.Close();
        }
示例#11
0
        public static string GetColor(HSSFColor color)
        {
            StringBuilder stringBuilder = new StringBuilder(7);

            stringBuilder.Append('#');
            foreach (short s in color.GetTriplet())
            {
                //if (s < 10)
                //    stringBuilder.Append('0');

                stringBuilder.Append(s.ToString("x2"));
            }
            string result = stringBuilder.ToString();

            if (result.Equals("#ffffff"))
            {
                return("white");
            }

            if (result.Equals("#c0c0c0"))
            {
                return("silver");
            }

            if (result.Equals("#808080"))
            {
                return("gray");
            }

            if (result.Equals("#000000"))
            {
                return("black");
            }

            return(result);
        }
        static CellFormatPart()
        {
            NAMED_COLORS = new Dictionary <String, Color>(CASE_INSENSITIVE_ORDER);

            Hashtable colors = HSSFColor.GetIndexHash();

            foreach (object v in colors.Values)
            {
                HSSFColor hc   = (HSSFColor)v;
                Type      type = hc.GetType();
                String    name = type.Name;
                if (name.Equals(name.ToUpper()))
                {
                    short[] rgb = hc.GetTriplet();
                    Color   c   = Color.FromArgb(rgb[0], rgb[1], rgb[2]);
                    if (!NAMED_COLORS.ContainsKey(name))
                    {
                        NAMED_COLORS.Add(name, c);
                    }
                    if (name.IndexOf('_') > 0)
                    {
                        if (!NAMED_COLORS.ContainsKey(name.Replace('_', ' ')))
                        {
                            NAMED_COLORS.Add(name.Replace('_', ' '), c);
                        }
                    }
                    if (name.IndexOf("_PERCENT") > 0)
                    {
                        if (!NAMED_COLORS.ContainsKey(name.Replace("_PERCENT", "%").Replace('_', ' ')))
                        {
                            NAMED_COLORS.Add(name.Replace("_PERCENT", "%").Replace('_', ' '), c);
                        }
                    }
                }
            }
            // A condition specification
            String condition = "([<>=]=?|!=|<>)    # The operator\n" +
                               "  \\s*([0-9]+(?:\\.[0-9]*)?)\\s*  # The constant to test against\n";

            String color =
                "\\[(black|blue|cyan|green|magenta|red|white|yellow|color [0-9]+)\\]";

            // A number specification
            // Note: careful that in something like ##, that the trailing comma is not caught up in the integer part

            // A part of a specification
            String part = "\\\\.                 # Quoted single character\n" +
                          "|\"([^\\\\\"]|\\\\.)*\"         # Quoted string of characters (handles escaped quotes like \\\") \n" +
                          "|_.                             # Space as wide as a given character\n" +
                          "|\\*.                           # Repeating fill character\n" +
                          "|@                              # Text: cell text\n" +
                          "|([0?\\#](?:[0?\\#,]*))         # Number: digit + other digits and commas\n" +
                          "|e[-+]                          # Number: Scientific: Exponent\n" +
                          "|m{1,5}                         # Date: month or minute spec\n" +
                          "|d{1,4}                         # Date: day/date spec\n" +
                          "|y{2,4}                         # Date: year spec\n" +
                          "|h{1,2}                         # Date: hour spec\n" +
                          "|s{1,2}                         # Date: second spec\n" +
                          "|am?/pm?                        # Date: am/pm spec\n" +
                          "|\\[h{1,2}\\]                   # Elapsed time: hour spec\n" +
                          "|\\[m{1,2}\\]                   # Elapsed time: minute spec\n" +
                          "|\\[s{1,2}\\]                   # Elapsed time: second spec\n" +
                          "|[^;]                           # A character\n" + "";

            String format = "(?:" + color + ")?                  # Text color\n" +
                            "(?:\\[" + condition + "\\])?                # Condition\n" +
                            "((?:" + part + ")+)                        # Format spec\n";

            RegexOptions flags = RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled;

            COLOR_PAT         = new Regex(color, flags);
            CONDITION_PAT     = new Regex(condition, flags);
            SPECIFICATION_PAT = new Regex(part, flags);
            FORMAT_PAT        = new Regex(format, flags);

            // Calculate the group numbers of important groups.  (They shift around
            // when the pattern is Changed; this way we figure out the numbers by
            // experimentation.)

            COLOR_GROUP = FindGroup(FORMAT_PAT, "[Blue]@", "Blue");
            CONDITION_OPERATOR_GROUP = FindGroup(FORMAT_PAT, "[>=1]@", ">=");
            CONDITION_VALUE_GROUP    = FindGroup(FORMAT_PAT, "[>=1]@", "1");
            SPECIFICATION_GROUP      = FindGroup(FORMAT_PAT, "[Blue][>1]\\a ?", "\\a ?");
        }
示例#13
0
        /**
         * org.Openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt to
         * org.Openxmlformats.schemas.Drawingml.x2006.main.CTFont adapter
         */
        private static void ApplyAttributes(CT_RPrElt pr, CT_TextCharacterProperties rPr)
        {
            if (pr.SizeOfBArray() > 0)
            {
                rPr.b = (/*setter*/ pr.GetBArray(0).val);
            }
            if (pr.SizeOfUArray() > 0)
            {
                ST_UnderlineValues u1 = pr.GetUArray(0).val;
                if (u1 == ST_UnderlineValues.single)
                {
                    rPr.u = (/*setter*/ ST_TextUnderlineType.sng);
                }
                else if (u1 == ST_UnderlineValues.@double)
                {
                    rPr.u = (/*setter*/ ST_TextUnderlineType.dbl);
                }
                else if (u1 == ST_UnderlineValues.none)
                {
                    rPr.u = (/*setter*/ ST_TextUnderlineType.none);
                }
            }
            if (pr.SizeOfIArray() > 0)
            {
                rPr.i = (/*setter*/ pr.GetIArray(0).val);
            }

            if (pr.SizeOfRFontArray() > 0)
            {
                CT_TextFont rFont = rPr.IsSetLatin() ? rPr.latin : rPr.AddNewLatin();
                rFont.typeface = (/*setter*/ pr.GetRFontArray(0).val);
            }

            if (pr.SizeOfSzArray() > 0)
            {
                int sz = (int)(pr.GetSzArray(0).val * 100);
                rPr.sz = (/*setter*/ sz);
            }

            if (pr.SizeOfColorArray() > 0)
            {
                CT_SolidColorFillProperties fill = rPr.IsSetSolidFill() ? rPr.solidFill : rPr.AddNewSolidFill();
                NPOI.OpenXmlFormats.Spreadsheet.CT_Color xlsColor = pr.GetColorArray(0);
                if (xlsColor.IsSetRgb())
                {
                    CT_SRgbColor clr = fill.IsSetSrgbClr() ? fill.srgbClr : fill.AddNewSrgbClr();
                    clr.val = (/*setter*/ xlsColor.rgb);
                }
                else if (xlsColor.IsSetIndexed())
                {
                    HSSFColor indexed = (HSSFColor)HSSFColor.GetIndexHash()[((int)xlsColor.indexed)];
                    if (indexed != null)
                    {
                        byte[] rgb = new byte[3];
                        rgb[0] = (byte)indexed.GetTriplet()[0];
                        rgb[1] = (byte)indexed.GetTriplet()[1];
                        rgb[2] = (byte)indexed.GetTriplet()[2];
                        CT_SRgbColor clr = fill.IsSetSrgbClr() ? fill.srgbClr : fill.AddNewSrgbClr();
                        clr.val = (/*setter*/ rgb);
                    }
                }
            }
        }
示例#14
0
        private static void ApplyAttributes(CT_RPrElt pr, CT_TextCharacterProperties rPr)
        {
            if (pr.sizeOfBArray() > 0)
            {
                rPr.b = pr.GetBArray(0).val;
            }
            if (pr.sizeOfUArray() > 0)
            {
                switch (pr.GetUArray(0).val)
                {
                case ST_UnderlineValues.none:
                    rPr.u = ST_TextUnderlineType.none;
                    break;

                case ST_UnderlineValues.single:
                    rPr.u = ST_TextUnderlineType.sng;
                    break;

                case ST_UnderlineValues.@double:
                    rPr.u = ST_TextUnderlineType.dbl;
                    break;
                }
            }
            if (pr.sizeOfIArray() > 0)
            {
                rPr.i = pr.GetIArray(0).val;
            }
            if (pr.sizeOfFamilyArray() > 0)
            {
                rPr.AddNewLatin().typeface = pr.GetRFontArray(0).val;
            }
            if (pr.sizeOfSzArray() > 0)
            {
                int num = (int)(pr.GetSzArray(0).val * 100.0);
                rPr.sz = num;
            }
            if (pr.sizeOfColorArray() <= 0)
            {
                return;
            }
            CT_SolidColorFillProperties colorFillProperties = rPr.IsSetSolidFill() ? rPr.solidFill : rPr.AddNewSolidFill();

            NPOI.OpenXmlFormats.Spreadsheet.CT_Color colorArray = pr.GetColorArray(0);
            if (colorArray.IsSetRgb())
            {
                (colorFillProperties.IsSetSrgbClr() ? colorFillProperties.srgbClr : colorFillProperties.AddNewSrgbClr()).val = colorArray.rgb;
            }
            else
            {
                if (!colorArray.IsSetIndexed())
                {
                    return;
                }
                HSSFColor hssfColor = HSSFColor.GetIndexHash()[(object)(int)colorArray.indexed] as HSSFColor;
                if (hssfColor == null)
                {
                    return;
                }
                byte[] numArray = new byte[3] {
                    (byte)hssfColor.GetTriplet()[0], (byte)hssfColor.GetTriplet()[1], (byte)hssfColor.GetTriplet()[2]
                };
                (colorFillProperties.IsSetSrgbClr() ? colorFillProperties.srgbClr : colorFillProperties.AddNewSrgbClr()).val = numArray;
            }
        }