Issues with Custom Colors

Hi,

We need to set the worksheet tab color using various custom colors. The worksheets are created from templates that contain smart markers. Each template’s worksheet tab is set to a specific custom color.

Initially, I thought I could simply use Worksheet.Copy, and the tab color would be preserved. However, occasionally the tab color ends up incorrect, and I’m not sure why.

To work around this, I now set the TabColor programmatically using Color.FromArgb with the custom RGB values. But occasionally, Color.FromArgb seems to return an incorrect color for the same RGB input. For example, #76933C should correspond to ARGB (255, 118, 147, 60), but I sometimes get ARGB (255, 89, 110, 45) instead. This usually happens to the last worksheet of my workbook.

Any idea why I’m not getting consistent colors?

Thank you,
Louis

@sanshi

Cause

The issue you’re experiencing with inconsistent worksheet tab colors when using Color.FromArgb may stem from how colors are being interpreted or applied in the context of your workbook. Sometimes, copying worksheets can lead to unexpected results if the source and destination formats differ.

Solution

To ensure consistent tab colors, it’s essential to explicitly set the tab color for each worksheet after copying. This approach helps avoid any discrepancies that might arise from the copying process.

Here’s a code snippet that demonstrates how to set the tab color programmatically:

// Instantiate a new Workbook
Workbook workbook = new Workbook("path/to/your/template.xlsx");

// Copy the first worksheet to a new one
Worksheet sourceWorksheet = workbook.Worksheets[0];
Worksheet newWorksheet = workbook.Worksheets.Add("NewSheet");
newWorksheet.Copy(sourceWorksheet);

// Set the tab color using Color.FromArgb
newWorksheet.TabColor = Color.FromArgb(255, 118, 147, 60); // For #76933C

// Save the workbook
workbook.Save("path/to/save/your/output.xlsx");

Next Steps

  1. Ensure that you are setting the tab color immediately after copying the worksheet.
  2. If the issue persists, check if there are any specific settings or properties in your templates that might affect color rendering.
  3. Test with different RGB values to see if the issue is isolated to specific colors.

If you follow these steps and the problem continues, please provide additional details about your environment or any error messages you encounter. I’m here to help!

My logic is similar to the above. But during debugging, I observe that Color.FromArgb returns a wrong value for the same inputs (255, 118, 147, 60) so newWorksheet.TabColor is assigned a wrong value. I don’t know why this happens.

Ok, after further debugging and refactoring my code, I found out that the following TabColor assignment line caused the issue:

var clr = Color.FromArgb(255, 118, 147, 60);
wksheet.TabColor = clr;

Where wksheet.TabColor is not same as clr for some sheets: clr has (255, 118, 147, 60), but wksheet.TabColor has (255, 89, 110, 45)!

Please let me know how to fix this issue. Thanks.

@sanshi
I tried the following codes ,it works fine.

Workbook workbook = new Workbook();
 var clr = Color.FromArgb(255, 118, 147, 60);
 workbook.Worksheets[0].TabColor = clr;
 workbook.Save(dir + "dest.xlsx");

result:
tabcolor.png (29.7 KB)

So could you share your template file and codes ? We will check it ASAP.

Simon,

Thank you for the reply. The logic you showed works most of the time. But occasionally, one of tabs would have a wrong color.

Sorry I’m not able to share any templates because it’s against company’s policy.

I asked DS about this question. DS suggests me to use palette colors instead. I don’t know if this would work or not. But I will give it a try on Monday.

@sanshi
You can remove all data in the template file, then check whether this issue will happen again.
If yes, please share the desensitized file.
And which version are you using?

The problem is that I cannot send out any files from my company computer.

Hopefully the palette colors would work.

@sanshi
Could you try the latest version 25.7?
There was an issue for setting color when the old color is theme color in the old version.

Thanks for the reply. We are always behind. I don’t know when we’ll have 25.x. For the time being, I recreated some templates from the scratch. I don’t see bad tab colors anymore.

Thank you for all the help.

@sanshi
Thank you for your feedback. I’m glad your issue has been resolved. If you have any questions, please feel free to contact us at any time.

In addition, Cells will release a version in the first half of each month, with version numbers combined by year and month. You can download and view the release version through the following link.