Rich Text Format: Documents Background Color


is there anyone in the forum who has already come up with the same question? In Microsoft Word you can set the document background color. It will also be exported to RTF files. The RTF code for the background color looks like this.

{\\*\\background {\\shp{\\*\\shpinst\\shpleft0\\shptop0\\shpright0\\shpbottom0\\shpfhdr0\\shpbxmargin\\shpbymargin\\shpwr0\\shpwrk0\\shpfblwtxt1\\shpz0\\shplid1025{\\sp{\\sn shapeType}{\\sv 1}}{\\sp{\\sn fFlipH}{\\sv 0}}{\\sp{\\sn fFlipV}{\\sv 0}}{\\sp{\\sn fillColor}{\\sv 1261823}}{\\sp{\\sn fFilled}{\\sv 1}}{\\sp{\\sn lineWidth}{\\sv 0}}{\\sp{\\sn fLine}{\\sv 0}}{\\sp{\\sn bWMode}{\\sv 9}}{\\sp{\\sn fBackground}{\\sv 1}}}}}

Unfortunately, the RTF specification does not provide an exact specification of how the color value is coded. The only thing I could filter out of the code so far is that {\sp{\sn fillColor}{\sv 1261823}} is responsible for the background color. If you play a little with the numerical value, the background color of the RTF file also changes. Does anyone know in which format the color value is coded? I would like to read it out and pass it to a color variable in Xojo.

Thank you!

1261823 in hex is &h1340FF
aren’t they the R G and B values on 8 bits ?

Unfortunately, it’s not that simple. The background color in the test document is set to red. You can download the RTF here. If I enter your value into a color converter, I get a blue tone.

&hFF4013 gives you a beautiful red.
may be you could give us some samples: this number gives this color, so that we can search ?

well it is

change it to 0 and you get black
change it to 65535 you get yellow
65280 (00FF00) is green
16777215 is white (FFFFFF)

but it is NOT RGB… seems it is BGR because 16711680 (FF0000) returns BLUE

I remember from VBA times that Office uses a long for RGB. For instance mentioned here: . You may have to Goggle for the exact formula.

Got it:

@Dave S Thanks. How did you convert the color?

what do you mean how did I convert the color?
I entered values for colors that I knew, and observed the results. when I entered 16711680 I expected RED but got BLUE therefore the Red and Blue bytes are transposed… once I determined that all other values resulted in the expected color

Blue0xFF0000 + Green0x00FF00 + Red

Forget my question, I have now understood how you came to the respective values. Thank you very much, that is the solution to the riddle.

Sorry, ones again: I understand how to convert this binary value using Hex function to readable color code. What does the calculation of a color given in Xojo look like in this binary format?

for the most part a color is a 24 bit number… a number is a number as far as the computer is concerned. the digits 0-9, and hex representation of for the convience of people

12345678 is 0xBC614E in HEX and 101111000110000101001110 in Binary

Xojo uses RGB where R=Red as a value between 0-255 [0x00 to 0xFF], G=Green, and B=Blue (seems RTF flips Red and Blue)

Xojo RED would be RGB(255,0,0) or &CFF0000

not sure if any of that answers you question

Suppose I have this color (purple):

Dim c As Color = &c470bb7 ' -> {\\sp{\\sn fillColor}{\\sv ???}}

How do I calculate the numeric value and then write it to an RTF file as above (???)?

[quote=425303:@Martin Trippensee]Suppose I have this color (purple):

Dim c As Color = &c470bb7 ' -> {\\sp{\\sn fillColor}{\\sv ???}}

How do I calculate the numeric value and then write it to an RTF file as above (???)?[/quote]

(&H47 * 65536) + (&0B * 256) + (&b7)

(71 * 65536 ) + (11 * 256 ) + ( 183)
(4653056) + ( 2816) + (183) = 4656055

Simple Hex to Decimal conversion


dim x as Integer = &H470bb7 // use H not C
msgbox str(x)

In other words

Public Function toBGRInteger(extends c as color) as Integer return c.Red+c.Green*256+c.Blue*65536 End Function

OK, i made some test. Using the following code and enter the returned rtfColorCode into the RTF, returns not the same Color in Microsoft Word. Looks like there are small differences in calculation:

[code]Dim c As Color = &c470bb7

Dim r As Integer = c.Red * 65536
Dim g As Integer = c.Green * 256
Dim b As Integer = c.Blue

Rim rtfColorCode As Integer = r + g + b ’ Result: 4656055, open it with Word returns &cB70B47[/code]

You mixed up red and blue values. The Integer result for &c470bb7 is 11995975.

actually 0x470bb7 is 4656055, but what he really wants is 0xb70b47 which is 11995975

like you said he neglected to transpose the Red and Blue bytes… Xojo (and the rest of the world) is R-G-B
but seems Word is B-G-R

so the function you posted will transform an Xojo Color to a Word Color

Thanks David and Ulrich. I can live with the small difference between the color Xojo returns and the color generated by Word from the given Xojo Integer.

difference… what difference?

The only differnce is that Word swaps the Red and Blue bytes… .otherwise they should render the exact same colors (unless Word uses a non-standard colorspace)