question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Theme XLColor to System.Drawing.Color

See original GitHub issue

Is there a way to convert a cell’s fill background color if it is a theme ColorType to system.drawing.color ?

XLColor c = XLColor.FromTheme(cell.Style.Fill.BackgroundColor.ThemeColor, cell.Style.Fill.BackgroundColor.ThemeTint);
return c.Color;

fails : "Cannot convert theme color to Color."

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
igiturcommented, Feb 17, 2017

Look at #210 :

workbook.Theme.ResolveThemeColor(XLThemeColor themeColor)
1reaction
Moonlgumcommented, Feb 15, 2017

A method in ClosedXml would be nice to have. The color won’t be correct until it has the theme color tint applied to it.

Something like this is close to the System.Color returned from Excel Interop for a themed cell.

       private System.Drawing.Color GetColor(IXLCell cell)
    {
        if (cell.Style.Fill.BackgroundColor.ColorType == XLColorType.Color)
        {
            return cell.Style.Fill.BackgroundColor.Color;
        }
        if (cell.Style.Fill.BackgroundColor.ColorType == XLColorType.Indexed)
        {
            XLColor c = XLColor.FromIndex(cell.Style.Fill.BackgroundColor.Indexed);
            System.Drawing.Color color = c.Color;
            return color;
        }
        if (cell.Style.Fill.BackgroundColor.ColorType == XLColorType.Theme)
        {
            XLWorkbook workbook = cell.Worksheet.Workbook;

            try
            {
                double tint = cell.Style.Fill.BackgroundColor.ThemeTint;
                XLThemeColor themeColor = cell.Style.Fill.BackgroundColor.ThemeColor;
                
                System.Drawing.Color color;
                if (themeColor == XLThemeColor.Accent1)
                {
                    color = workbook.Theme.Accent1.Color;
                }
                else if (themeColor == XLThemeColor.Accent2)
                {
                    color = workbook.Theme.Accent2.Color;
                }
                else if (themeColor == XLThemeColor.Accent3)
                {
                    color = workbook.Theme.Accent3.Color;
                }
                else if (themeColor == XLThemeColor.Accent4)
                {
                    color = workbook.Theme.Accent4.Color;
                }
                else if (themeColor == XLThemeColor.Accent5)
                {
                    color = workbook.Theme.Accent5.Color;
                }
                else if (themeColor == XLThemeColor.Accent6)
                {
                    color = workbook.Theme.Accent6.Color;
                }
                else if (themeColor == XLThemeColor.Background1)
                {
                    color = workbook.Theme.Background1.Color;
                }
                else if (themeColor == XLThemeColor.Background2)
                {
                    color = workbook.Theme.Background2.Color;
                }
                else if (themeColor == XLThemeColor.FollowedHyperlink)
                {
                    color = workbook.Theme.FollowedHyperlink.Color;
                }
                else if (themeColor == XLThemeColor.Hyperlink)
                {
                    color = workbook.Theme.Hyperlink.Color;
                }
                else if (themeColor == XLThemeColor.Text1)
                {
                    color = workbook.Theme.Text1.Color;
                }
                else if (themeColor == XLThemeColor.Text2)
                {
                    color = workbook.Theme.Text2.Color;
                }
                return color.ApplyTint(tint);
            }
            finally
            {
                workbook?.Dispose();
            }
        }
    }
	
	
    public static Color ApplyTint(this Color color, double tint)
    {
        if (tint < -1.0) tint = -1.0;
        if (tint > 1.0) tint = 1.0;

        Color colorRgb = color;
        double fHue = colorRgb.GetHue();
        double fSat = colorRgb.GetSaturation();
        double fLum = colorRgb.GetBrightness();
        if (tint < 0)
        {
            fLum = fLum * (1.0 + tint);
        }
        else
        {
            fLum = fLum * (1.0 - tint) + (1.0 - 1.0 * (1.0 - tint));
        }
        return ToColor(fHue, fSat, fLum);
    }

    internal static Color ToColor(double hue, double saturation, double luminance)
    {
        double chroma = (1.0 - Math.Abs(2.0 * luminance - 1.0)) * saturation;
        double fHue = hue / 60.0;
        double fHueMod2 = fHue;
        while (fHueMod2 >= 2.0) fHueMod2 -= 2.0;
        double fTemp = chroma * (1.0 - Math.Abs(fHueMod2 - 1.0));

        double fRed, fGreen, fBlue;
        if (fHue < 1.0)
        {
            fRed = chroma;
            fGreen = fTemp;
            fBlue = 0;
        }
        else if (fHue < 2.0)
        {
            fRed = fTemp;
            fGreen = chroma;
            fBlue = 0;
        }
        else if (fHue < 3.0)
        {
            fRed = 0;
            fGreen = chroma;
            fBlue = fTemp;
        }
        else if (fHue < 4.0)
        {
            fRed = 0;
            fGreen = fTemp;
            fBlue = chroma;
        }
        else if (fHue < 5.0)
        {
            fRed = fTemp;
            fGreen = 0;
            fBlue = chroma;
        }
        else if (fHue < 6.0)
        {
            fRed = chroma;
            fGreen = 0;
            fBlue = fTemp;
        }
        else
        {
            fRed = 0;
            fGreen = 0;
            fBlue = 0;
        }

        double fMin = luminance - 0.5 * chroma;
        fRed += fMin;
        fGreen += fMin;
        fBlue += fMin;

        fRed *= 255.0;
        fGreen *= 255.0;
        fBlue *= 255.0;

        var red = Convert.ToInt32(Math.Truncate(fRed));
        var green = Convert.ToInt32(Math.Truncate(fGreen));
        var blue = Convert.ToInt32(Math.Truncate(fBlue));

        red = Math.Min(255, Math.Max(red, 0));
        green = Math.Min(255, Math.Max(green, 0));
        blue = Math.Min(255, Math.Max(blue, 0));

        return Color.FromArgb(red, green, blue);
    }		
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert System.Drawing.Color to ClosedXML.Excel ...
My question is: Is there appropriate method to convert System.Drawing.Color to ClosedXML.Excel.XLColor? Any comments or suggestions are welcome.
Read more >
Color.FromArgb Method (System.Drawing)
Creates a Color structure from the four ARGB component (alpha, red, green, and blue) values. Although this method allows a 32-bit value to...
Read more >
ColorTranslator.FromHtml(String) Method (System.Drawing)
This method translates a string representation of an HTML color name, such as Blue or Red, to a GDI+ Color structure. Applies to....
Read more >
How to convert System.Drawing.Color to ClosedXML.Excel ...
I've never used ClosedXml but the fine manual shows many ways an XLColor can be created. I picked on the first when writing...
Read more >
【.NET 5】【WPF】【ClosedXML】Draw and print spreadsheets 1
Fonts and Borders have two color types. When their color types are "Color", I can get color from "XLColor.Color". But if they are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found