Skip to content

Rare Problem with Indexed Colors #2499

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

Closed
oleibman opened this issue Jan 13, 2022 · 0 comments · Fixed by #2595
Closed

Rare Problem with Indexed Colors #2499

oleibman opened this issue Jan 13, 2022 · 0 comments · Fixed by #2595

Comments

@oleibman
Copy link
Collaborator

This is:

- [x] a bug report
- [ ] a feature request
- [x] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

This is an offshoot of #2490. In that issue, the user reported that PhpSpreadsheet could not read an xls file, and the reason was because the file was in BIFF4 format, unsupported by PhpSpreadsheet, and basically obsolete since 1993. I suggested to use a conversion routine that comes with Excel to convert the file to xlsx format. I will upload the resulting file, which was satisfactory for the user.

But ... when I used PhpSpreadsheet to load and save that resulting file, the fill colors were wrong. Most of the cells use a solid yellow fill which is not present in the original. So, PhpSpreadsheet appears to have a bug, but ... not so fast! When I copy/paste the white background cell from A3 to a new spreadsheet, it arrives in the destination with a yellow background! So, something about the spreadsheet says "yellow background" to both Excel and PhpSpreadsheet, even though it has a white background.

I originally thought the problem had something to do with themes, but, when I substituted the theme from the original to the one created by PhpSpreadsheet, the yellow background was still there. Looking deeper, I believe I found the cause of the problem. The original styles.xml file contains a list of 64 indexed colors. This section is not typically found in the styles.xml file. Instead, a default set of indexed colors, numbered from 0 to 56, is used. When we encounter a color reference to index n, we actually use index n-7 from the default list. Not only is the list included in our problem file different from the default at several points, but it is also 7 entries longer, apparently at the beginning of the list, and I believe that the intention is that index n should be used, not n-7. The fill colors are described as indexes 13, 9, and 11; when the normal set is used these become mapped to indexes 6, 2, and 4, which are colors yellow, white, and green, which corresponds to how PhpSpreadsheet (and Excel when copying from this workbook to another) treat them. However, the colors actually used are F0FBFF (which is index 9 in this scheme), white (FFFFFF index 13 among others), and F0F0F0 (index 11); I think it is no coincidence that these are the precise color indexes which wind up mismapped.

I imagine that, knowing the problem, there are ways to deal with it. However, since this will probably involve changes to areas which I'm already changing in PR #2471, and since it seems to be a very rare problem, I am going to defer thinking about this one till after that has merged.

What is the current behavior?

See 'expected behavior'.

What are the steps to reproduce?

See 'expected behavior'.

Which versions of PhpSpreadsheet and PHP are affected?

All.

issue.2490.xlsx

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Feb 16, 2022
Fix PHPOffice#2499, which see for details of an obscure problem affecting both PhpSpreadsheet and Excel. Add support for palette contained in workbook styles. This seems to be a very rare occurrence, so allow it only when the palette contains exactly 64 entries. If there are other possibilities, we'll presumably have a new workbook to guide us how to handle them. Also add some tests for specification of indexed color without palette, another rarity (no in-range examples amongst our current files). Also change one private static array, initialized once at run-time and never changed, to a constant.
oleibman added a commit that referenced this issue Feb 24, 2022
Fix #2499, which see for details of an obscure problem affecting both PhpSpreadsheet and Excel. Add support for palette contained in workbook styles. This seems to be a very rare occurrence, so allow it only when the palette contains exactly 64 entries. If there are other possibilities, we'll presumably have a new workbook to guide us how to handle them. Also add some tests for specification of indexed color without palette, another rarity (no in-range examples amongst our current files). Also change one private static array, initialized once at run-time and never changed, to a constant.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant