From: Rick Rothstein on 6 Feb 2010 13:35 To make it easier for those wanting you to use your formula in their own code, here it is cast into a function that takes the Long value normally returned by the various controls' BackColor properties... Function TextColorToUse(BackColor As Long) As Long Dim Luminance As Long Luminance = 77 * (BackColor Mod &H100) + _ 151 * ((BackColor \ &H100) Mod &H100) + _ 28 * ((BackColor \ &H10000) Mod &H100) ' Default value of TextColorToUse is 0-Black, set ' it to White if the Luminance is less than 32640 If Luminance < 32640 Then TextColorToUse = vbWhite End Function And for those who expect such things from me, here's the one-liner version of the above function... Function TextColorToUse2(BackColor As Long) As Long TextColorToUse2 = -vbWhite * (77 * (BackColor Mod &H100) + _ 151 * ((BackColor \ &H100) Mod &H100) + 28 * _ ((BackColor \ &H10000) Mod &H100) < 32640) End Function -- Rick (MVP - Excel) "Jim Mack" wrote in message news:e8yBwytpKHA.3912(a)TK2MSFTNGP06.phx.gbl...> Claire wrote: >> Thank you, Kevin. >> That is a good start. >> I am using common dialog API and I can see those values:Hue, Sat & >> Lum displayed in the dialog window. >> Is there any way to to get those values through API? >> Claire > > You don't need to go quite that far -- just calculate the luminance. > That's pretty simple: > > Dim Y As Long, R As Long, etc > > Y = (B * 28&) + (R * 77&) + (G * 151&) > > Where BRG are the Blue, Red & Green components, with values from > 0..255 each. > > Y will be the Luminance value, from 0..65280. > > If Y >= 32640, use Black as the forecolor. If less, use White. > > -- > Jim Mack > Twisted tees at http://www.cafepress.com/2050inc > "We sew confusion" > From: Jim Mack on 6 Feb 2010 15:35 Rick Rothstein wrote:> To make it easier for those wanting you to use your formula in > their own code, here it is cast into a function that takes the Long > value normally returned by the various controls' BackColor > properties... Yeah, I didn't go there because it gets complicated by palletized colors and system colors. IOW if your .BackColor returns &h8000000F you need to run that through GetSysColor to get the current BGR value for that system color index, etc. But one-liners are always appreciated. (-: -- Jim Mack Twisted tees at http://www.cafepress.com/2050inc "We sew confusion" > > Function TextColorToUse(BackColor As Long) As Long > Dim Luminance As Long > Luminance = 77 * (BackColor Mod &H100) + _ > 151 * ((BackColor \ &H100) Mod &H100) + _ > 28 * ((BackColor \ &H10000) Mod &H100) > ' Default value of TextColorToUse is 0-Black, set > ' it to White if the Luminance is less than 32640 > If Luminance < 32640 Then TextColorToUse = vbWhite > End Function > > And for those who expect such things from me, here's the one-liner > version of the above function... > > Function TextColorToUse2(BackColor As Long) As Long > TextColorToUse2 = -vbWhite * (77 * (BackColor Mod &H100) + _ > 151 * ((BackColor \ &H100) Mod &H100) + 28 * _ > ((BackColor \ &H10000) Mod &H100) < 32640) > End Function > > > "Jim Mack" wrote in message > news:e8yBwytpKHA.3912(a)TK2MSFTNGP06.phx.gbl... >> Claire wrote: >>> Thank you, Kevin. >>> That is a good start. >>> I am using common dialog API and I can see those values:Hue, Sat & >>> Lum displayed in the dialog window. >>> Is there any way to to get those values through API? >>> Claire >> >> You don't need to go quite that far -- just calculate the >> luminance. That's pretty simple: >> >> Dim Y As Long, R As Long, etc >> >> Y = (B * 28&) + (R * 77&) + (G * 151&) >> >> Where BRG are the Blue, Red & Green components, with values from >> 0..255 each. >> >> Y will be the Luminance value, from 0..65280. >> >> If Y >= 32640, use Black as the forecolor. If less, use White. >> >> -- >> Jim Mack >> Twisted tees at http://www.cafepress.com/2050inc >> "We sew confusion" From: Rick Rothstein on 6 Feb 2010 17:26 What are "palletized" colors? We have something like that (I think) in Excel, but in Excel's model you can return either the ColorIndex (which is an index into a "pallet" of colors) or the Color (which is the normal Long value for the color), so in Excel there is no problem with this. However, yes, you raise a good point about system colors though. While this negates being able to make a one-liner, the fix is actually quite simple... Function TextColorToUse(BackColor As Long) As Long Dim Luminance As Long If BackColor < 0 Then MsgBox "Sorry, but that is a System Color... I don't do System Colors!" Else Luminance = 77 * (BackColor Mod &H100) + _ 151 * ((BackColor \ &H100) Mod &H100) + _ 28 * ((BackColor \ &H10000) Mod &H100) If Luminance < 32640 Then TextColorToUse = vbWhite End If End Function -- Rick (MVP - Excel) "Jim Mack" wrote in message news:eq0llv2pKHA.1672(a)TK2MSFTNGP04.phx.gbl...> Rick Rothstein wrote: >> To make it easier for those wanting you to use your formula in >> their own code, here it is cast into a function that takes the Long >> value normally returned by the various controls' BackColor >> properties... > > Yeah, I didn't go there because it gets complicated by palletized > colors and system colors. IOW if your .BackColor returns &h8000000F > you need to run that through GetSysColor to get the current BGR value > for that system color index, etc. > > But one-liners are always appreciated. (-: > > -- > Jim Mack > Twisted tees at http://www.cafepress.com/2050inc > "We sew confusion" > > > > >> >> Function TextColorToUse(BackColor As Long) As Long >> Dim Luminance As Long >> Luminance = 77 * (BackColor Mod &H100) + _ >> 151 * ((BackColor \ &H100) Mod &H100) + _ >> 28 * ((BackColor \ &H10000) Mod &H100) >> ' Default value of TextColorToUse is 0-Black, set >> ' it to White if the Luminance is less than 32640 >> If Luminance < 32640 Then TextColorToUse = vbWhite >> End Function >> >> And for those who expect such things from me, here's the one-liner >> version of the above function... >> >> Function TextColorToUse2(BackColor As Long) As Long >> TextColorToUse2 = -vbWhite * (77 * (BackColor Mod &H100) + _ >> 151 * ((BackColor \ &H100) Mod &H100) + 28 * _ >> ((BackColor \ &H10000) Mod &H100) < 32640) >> End Function >> >> >> "Jim Mack" wrote in message >> news:e8yBwytpKHA.3912(a)TK2MSFTNGP06.phx.gbl... >>> Claire wrote: >>>> Thank you, Kevin. >>>> That is a good start. >>>> I am using common dialog API and I can see those values:Hue, Sat & >>>> Lum displayed in the dialog window. >>>> Is there any way to to get those values through API? >>>> Claire >>> >>> You don't need to go quite that far -- just calculate the >>> luminance. That's pretty simple: >>> >>> Dim Y As Long, R As Long, etc >>> >>> Y = (B * 28&) + (R * 77&) + (G * 151&) >>> >>> Where BRG are the Blue, Red & Green components, with values from >>> 0..255 each. >>> >>> Y will be the Luminance value, from 0..65280. >>> >>> If Y >= 32640, use Black as the forecolor. If less, use White. >>> >>> -- >>> Jim Mack >>> Twisted tees at http://www.cafepress.com/2050inc >>> "We sew confusion" > From: Jim Mack on 6 Feb 2010 21:09 Rick Rothstein wrote:> What are "palletized" colors? We have something like that (I think) > in Excel, but in Excel's model you can return either the ColorIndex > (which is an index into a "pallet" of colors) or the Color (which > is the normal Long value for the color), so in Excel there is no > problem with this. However, yes, you raise a good point about > system colors though. While this negates being able to make a > one-liner, the fix is actually quite simple... > > MsgBox "Sorry, but that is a System Color... I don't do System > Colors! Yes, a much simpler fix than adding an API call. (-: I don't feel like digging into the docs right now, so I'm probably going to get something wrong and if so I hope Mike or Mike will correct me. Colors in the form 0x80... are system colors and the low byte is an index into the system color table. GetSysColor retrieves the RGB value at that index. Colors in the form 0x02... (and 0x01...?) are palettized, and the low part is an index into a logical palette -- I don't remember what the context is. I believe that every DC can have its own logical palette, which defaults to the system palette. This may be a non-issue on modern systems because everyone uses 8-8-8 color now, right? But on older systems, especially 256 color modes, palettes played an important role. -- Jim Mack Twisted tees at http://www.cafepress.com/2050inc "We sew confusion" From: C. Kevin Provance on 6 Feb 2010 21:16 I think the API to translate system colors is OleTranslateColor. "Jim Mack" wrote in message news:uE1zmq5pKHA.4648(a)TK2MSFTNGP06.phx.gbl... | Rick Rothstein wrote: | > What are "palletized" colors? We have something like that (I think) | > in Excel, but in Excel's model you can return either the ColorIndex | > (which is an index into a "pallet" of colors) or the Color (which | > is the normal Long value for the color), so in Excel there is no | > problem with this. However, yes, you raise a good point about | > system colors though. While this negates being able to make a | > one-liner, the fix is actually quite simple... | > | > MsgBox "Sorry, but that is a System Color... I don't do System | > Colors! | | Yes, a much simpler fix than adding an API call. (-: | | I don't feel like digging into the docs right now, so I'm probably | going to get something wrong and if so I hope Mike or Mike will | correct me. | | Colors in the form 0x80... are system colors and the low byte is an | index into the system color table. GetSysColor retrieves the RGB value | at that index. | | Colors in the form 0x02... (and 0x01...?) are palettized, and the low | part is an index into a logical palette -- I don't remember what the | context is. I believe that every DC can have its own logical palette, | which defaults to the system palette. | | This may be a non-issue on modern systems because everyone uses 8-8-8 | color now, right? But on older systems, especially 256 color modes, | palettes played an important role. | | -- | Jim Mack | Twisted tees at http://www.cafepress.com/2050inc | "We sew confusion" |