Bug 61993 - doc/docx auto font color inverted in LibreOffice
Summary: doc/docx auto font color inverted in LibreOffice
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
(earliest affected) release
Hardware: All All
: medium normal
Assignee: Luke Deller
Whiteboard: target:4.1.0
Depends on:
Reported: 2013-03-07 23:37 UTC by Luke Deller
Modified: 2014-03-17 13:51 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:

doc example (21.50 KB, application/msword)
2013-03-07 23:37 UTC, Luke Deller
matching docx example (12.63 KB, application/vnd.openxmlformats-officedocument.wordprocessingml.document)
2013-03-07 23:38 UTC, Luke Deller
doc example (fixed mime type) (21.50 KB, application/msword)
2013-03-07 23:40 UTC, Luke Deller
examples of what is considered "dark" across a range of colours (32.15 KB, application/vnd.oasis.opendocument.text)
2013-03-10 10:44 UTC, Luke Deller

Note You need to log in before you can comment on or make changes to this bug.
Description Luke Deller 2013-03-07 23:37:28 UTC
Created attachment 76135 [details]
doc example

The text in the attached document shows as white in Word 2010, but black in LibreOffice.

The text's colour is set to "automatic".  Both Word and LibreOffice interpret this by setting the colour to white on a dark background, and to black on a light background.  However they differ in what is considered to be a dark background.  This example has a background of dark grey (#333333) which is treated as dark by Word by not by LibreOffice.

Putting compatibility aside, Word's choice just looks better - subjectively this background looks dark, and the white text looks clearer on it than the black text.

I can see from LibreOffice source code that it considers the background to be dark if its luminance is <= 38.

I checked the ECMA-376 specification (4th edition) to see if this defines how automatic font colour should be shown for docx, but all it says is that the consumer can "automatically determine the run color as appropriate", "for example, in order to ensure that the run contents can be distinguished against the page's background color" (from
Comment 1 Luke Deller 2013-03-07 23:38:50 UTC
Created attachment 76136 [details]
matching docx example
Comment 2 Luke Deller 2013-03-07 23:40:01 UTC
Created attachment 76137 [details]
doc example (fixed mime type)
Comment 3 Luke Deller 2013-03-08 05:02:43 UTC
Word 2010 appears to treat a background colour as "dark" if:

130*red + 255*green * 51*blue < 105*255

Where red, green, blue are the RGB components of the background colour as 8-bit integers (ie in the range 0 to 255).

(I arrived at this formula by:
- collecting some data points by experimentation with Word 2010
- guessing the form of this formula
- using linear algebra with the data points to work out the coefficients
- double-checking using more data points)

By comparison, LibreOffice treats a background colour as "dark" if:

28*red + 151*green + 77*blue <= 38*256

(see Color::IsDark() in tools/source/generic/color.cxx, called from SwDrawTextInfo::ApplyAutoColor in sw/source/core/txtnode/fntcache.cxx)
Comment 4 Luke Deller 2013-03-08 14:09:34 UTC
Correction, the LibreOffice formula (from code) is:

77*red + 151*green + 28*blue <= 38*256

I notice that both Word and LibreOffice formulae use weights proportional to the "Rec. 601" coefficients for Luma shown here:
ie Luma = 0.299 R' + 0.587 G' + 0.114 B'

(Actually LibreOffice would match these coefficients more accurately if it used
 76*red + 151*green + 29*blue)

Anyway the main difference is that LibreOffice considers Luma <= 38 (out of 255) to be dark, whereas Word considers Luma < about 61 (out of 255) to be dark.

The formula I put forward earlier for Word is very slightly off, because of the following data points:
(red=61, green=61, blue=60) is not dark
(red=60, green=61, blue=61) is dark
Comment 5 Jorendc 2013-03-08 15:59:40 UTC
Mark it as NEW to 'delete' it from our UNCONFIRMED-list :).

Kind regards,
Comment 6 Luke Deller 2013-03-10 10:44:22 UTC
Created attachment 76263 [details]
examples of what is considered "dark" across a range of colours

Adding an example showing visually what is considered "dark" across a range of colours
Comment 7 Commit Notification 2013-03-11 14:57:13 UTC
Luke Deller committed a patch related to this issue.
It has been pushed to "master":


Change definition of "dark" colour for fdo#61993

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
Affected users are encouraged to test the fix and report feedback.
Comment 8 Björn Michaelsen 2014-03-17 12:41:50 UTC
@Luke: I assume this one can be closed as RESOLVED/FIXED?
Comment 9 Luke Deller 2014-03-17 13:51:20 UTC
Yes thanks Björn, closing it now.