Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reflections in HSSFColor #1158

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions main/HSSF/Util/HSSFColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ the License. You may obtain a copy of the License at
limitations Under the License.
==================================================================== */

// REMOVE-REFLECITON: Reflection is used to retrieve the 2nd index of a color. Refactored using virtual getter.

namespace NPOI.HSSF.Util
{
using NPOI.SS.UserModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
/**
* Intends to provide support for the very evil index to triplet Issue and
* will likely replace the color constants interface for HSSF 2.0.
Expand Down Expand Up @@ -99,7 +100,7 @@ private static Dictionary<int, HSSFColor> CreateColorsByIndexMap()
for (int i = 0; i < colors.Length; i++)
{
HSSFColor color = colors[i];
int index2 = GetIndex2(color);
int index2 = color.Indexed2;
if (index2 == -1)
{
// most colors don't have a second index
Expand All @@ -120,18 +121,6 @@ private static Dictionary<int, HSSFColor> CreateColorsByIndexMap()
return result;
}

private static int GetIndex2(HSSFColor color)
{
FieldInfo f = color.GetType().GetField("Index2", BindingFlags.Static | BindingFlags.Public);
if (f == null)
{
return -1;
}

short s = (short)f.GetValue(color);
return Convert.ToInt32(s);
}

internal static HSSFColor[] GetAllColors()
{
return new HSSFColor[] {
Expand Down Expand Up @@ -194,6 +183,7 @@ public virtual short Indexed
}
}

public virtual short Indexed2 => -1;
public byte[] RGB
{
get { return this.GetTriplet(); }
Expand Down Expand Up @@ -371,6 +361,7 @@ public class DarkBlue: HSSFColor
public static readonly byte[] Triplet = { 0, 0, 128 };
public const string HexString = "0:0:8080";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand Down Expand Up @@ -452,7 +443,7 @@ public class DarkRed: HSSFColor
public const short Index2 = 0x25;
public static readonly byte[] Triplet = { 128, 0, 0 };
public const string HexString = "8080:0:0";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand Down Expand Up @@ -561,7 +552,7 @@ public class Teal: HSSFColor
public const short Index2 = 0x26;
public static readonly byte[] Triplet = { 0, 128, 128 };
public const string HexString = "0:8080:8080";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand Down Expand Up @@ -589,7 +580,7 @@ public class Blue: HSSFColor
public const short Index2 = 0x27;
public static readonly byte[] Triplet = { 0, 0, 255 };
public const string HexString = "0:0:FFFF";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand Down Expand Up @@ -822,7 +813,7 @@ public class Violet: HSSFColor
public const short Index2 = 0x24;
public static readonly byte[] Triplet = { 128, 0, 128 };
public const string HexString = "8080:0:8080";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand Down Expand Up @@ -872,7 +863,7 @@ public class Pink: HSSFColor
public const short Index2 = 0x21;
public static readonly byte[] Triplet = { 255, 0, 255 };
public const string HexString = "FFFF:0:FFFF";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand Down Expand Up @@ -918,7 +909,7 @@ public class Yellow: HSSFColor
public const short Index2 = 0x22;
public static readonly byte[] Triplet = { 255, 255, 0 };
public const string HexString = "FFFF:FFFF:0";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand All @@ -941,7 +932,7 @@ public class BrightGreen: HSSFColor
public const short Index2 = 0x23;
public static readonly byte[] Triplet = { 0, 255, 0 };
public const string HexString = "0:FFFF:0";

public override short Indexed2 => Index2;
public override String GetHexString()
{
return HexString;
Expand Down Expand Up @@ -969,7 +960,7 @@ public class Turquoise: HSSFColor
public const short Index2 = 0x23;
public static readonly byte[] Triplet = { 0, 255, 255 };
public const string HexString = "0:FFFF:FFFF";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand Down Expand Up @@ -1024,7 +1015,7 @@ public class Plum: HSSFColor
public const short Index2 = 0x19;
public static readonly byte[] Triplet = { 153, 51, 102 };
public const string HexString = "9999:3333:6666";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand Down Expand Up @@ -1187,7 +1178,7 @@ public class LightTurquoise: HSSFColor
public const short Index2 = 0x1b;
public static readonly byte[] Triplet = { 204, 255, 255 };
public const string HexString = "CCCC:FFFF:FFFF";

public override short Indexed2 => Index2;
public override short Indexed
{
get{return Index;}
Expand Down
Loading