ms word - VBA to return RGB color of text -
i have been able return rgb colour of text in word document color has been picked using custom colours, not when standard colours used. used below code:
function getrgbtest(colour long) string getrgbtest = "rgb(" & (colour mod 256) & "," & ((colour \ 256) mod 256) & "," & ((colour \ 256 \ 256) mod 256) & ")" end function sub testcolour() msgbox getrgbtest(selection.font.color) end sub
when using standard colour, selection.font.color returns negative value , rgb value incorrect.
i have tried editing have following (where dict list of colorindex , respective rgb vals):
function getrgbtest(colour long, colind integer) string if colour > 0 getrgbtest = "rgb(" & (colour mod 256) & "," & ((colour \ 256) mod 256) & "," & ((colour \ 256 \ 256) mod 256) & ")" else colind = ltrim(str(colind)) getrgbtest = dict.item(colind) end if end function sub testcolour() msgbox getrgbtest(selection.font.color, selection.font.colorindex) end sub
although think colorindex returns value doesnt relate standard colours.
does have idea how transform these values rgb vals?
i haven't been able google reference, if recall correctly there 3 possible data formats word return font colour in.
rgb format
if high byte &h00
remaining 3 bytes represent red, green, , blue. format familiar , handling.
automatic colour
if value &hff000000
known -16777216
colour set automatic, , black. otherwise assumes default colour document.
mysterious third negative format
if high nibble of high byte &hd
, if first digit of hexidecimal representation of number d, uses word colour scheme format.
for example, might &d500ffff
second nibble, &h5
in our example, matches value in enumeration wdthemecolorindex. if create translation table translating enumeration msothemecolorschemeindex enumeration, can basic colours in document's activedocument.documenttheme.themecolorscheme
collection. why there 2 enumerations different index numbers same thing ask? question! moving on...
this not end of story however! there last 3 remaining bytes worry about! next one, low byte of high word, think it's easy. believe it's &h00
. if run different value byte... well, best of luck you.
the last 2 bytes represent percentages darken or lighten value (respectively). &ff
or 255 means no change , &h00
means 100%. same in colour picker see things "accent 2, lighter 60%". way &hd500ff66
, 5 being index accent 2, &h66
being 60% in lighter byte.
so here function not account lighter , darker values:
public function getbasicrgb(color long) string dim colorindexlookup dim colorindex integer dim finalcolor long colorindexlookup = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 1, 4, 3) colorindex = colorindexlookup( _ ((color , &hf000000) / &h1000000) _ + lbound(colorindexlookup)) finalcolor = activedocument.documenttheme.themecolorscheme(colorindex) getbasicrgb = "rgb(" & (finalcolor , &hff) & "," & _ (finalcolor / &h100 , &hff) & "," & _ ((finalcolor , &hff0000) / &h10000) & ")" end function
to account lighter , darker believe have convert rgb value hsl value modify l component % lighter or darker. convert rgb. don't care figure out right now.
Comments
Post a Comment