Conversation
wb_apply_cell_styles() and wb_apply_border() passed flextable's color
strings straight to openxlsx2::wb_color(). R writes a color with an alpha
channel as #RRGGBBAA, but Excel reads eight hex digits as AARRGGBB, so
the alpha byte became the red channel: a translucent yellow such as
"#EDB50199" (adjustcolor("#edb501", 0.6)) exported as purple, and a
half-transparent black border as dark blue.
Font colors were already normalised through prepare_color(); fills and
borders now go the same way. prepare_fill_color() wraps it so that
"transparent" survives as "transparent" for the existing no-fill check.
Adds a regression test asserting the opaque FFRRGGBB value for a fill
and a border with alpha, and that a named color and an unfilled cell are
unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #47
The problem
wb_apply_cell_styles()andwb_apply_border()pass flextable's colour strings straight toopenxlsx2::wb_color(). R writes a colour with an alpha channel as#RRGGBBAA, but Excel reads eight hex digits asAARRGGBB, so the alpha byte lands in the red channel and the colour shifts by one byte. A translucent yellow such as#EDB50199(adjustcolor("#edb501", 0.6)) exports as purple; a half-transparent black border exports as dark blue. Nothing errors, the colours are just wrong.Font colours do not suffer from this because
wb_apply_content()already normalises them throughprepare_color(). Fills and borders skipped that step.The fix
wb_apply_cell_styles()goes through a newprepare_fill_color(), a thin wrapper aroundprepare_color()that keeps"transparent"as"transparent"(whereprepare_color()returnsNA), so the existing!= "transparent"guard keeps working.border.color.*columns inwb_apply_border()go throughprepare_color()after the existingtransparent -> blacksubstitution.Both use
grDevices::col2rgb()under the hood, so named colours,#RRGGBBand#RRGGBBAAall reduce to the opaque#RRGGBBthatwb_color()turns intoFFRRGGBB. Excel has no translucent solid fills, so dropping the alpha is the only faithful option; it matches what font colours already do.Tests
Added a regression test in
test-wb_apply_cell_styles.Rthat exports a fill and a border with alpha and asserts the opaqueFFEDB501/FF000000in the style XML, that a named colour (orange) is unchanged, and that an unfilled cell stays unfilled. It fails onmain(Expected style_of("A2", "fill") to match "rgb=\"FFEDB501\"", actualrgb="EDB50199") and passes with the change. The full suite is green locally, with only the pre-existing CRAN-gated andflexlsxtestdirskips.NAMESPACEalready importsdplyr::coalesce, so no roxygen run is needed for the new helper.I added a NEWS bullet under a development heading; happy to drop or reword it.
Note: This fix was produced with the help of Claude