By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

Reversing a COlour Code

P: n/a
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users
to pick colours for various control properties in certain apps.

Is there a way to take the colour code that is displayed in a
backcolor/forecolor/etc property and calculate the "reverse colour"? In
other words, If a user picks 255 (red) for a control backcolor, I'd like
to be able to calculate the opposite or negative of that colour and
assign the control's forecolor property the new colour.

vbWhite and other colour constants are not an option, I don't think,
since the colour dialog allows a user to blend their own custom colour...

TIA for any help.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Dec 28 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Parse the color into bytes for red, green, and blue, using integer division
and Mod.
Flip the bits by NOTing them
Combine them with the RGB() function.

Assuming the RGB value is in a text box named txtCombined:
Dim btRed As Byte, btGreen As Byte, btBlue As Byte
Dim lngColor as Long
btRed = Me.txtCombined Mod 256
lngColor = Me.txtCombined \ 256
btGreen = lngColor Mod 256
btBlue = lngColor \ 256

btRed = Not btRed
'etc.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Tim Marshall" <TI****@PurplePandaChasers.Moertherium> wrote in message
news:do**********@coranto.ucs.mun.ca...
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to
pick colours for various control properties in certain apps.

Is there a way to take the colour code that is displayed in a
backcolor/forecolor/etc property and calculate the "reverse colour"? In
other words, If a user picks 255 (red) for a control backcolor, I'd like
to be able to calculate the opposite or negative of that colour and assign
the control's forecolor property the new colour.

vbWhite and other colour constants are not an option, I don't think, since
the colour dialog allows a user to blend their own custom colour...

TIA for any help.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto

Dec 28 '05 #2

P: n/a
Very early in the morning and without coffee my sense of logic says:

Not ColorCode And vbWhite
should reverse any RGB type color.

---

or

Not ColorCode And 16777215 if the constant vbWhite isn't available?

---

[Not ColorCode] should reverse all the bits.
[And vbWhite] should return the bits in the fourth byte to zero.

Dec 28 '05 #3

P: n/a
Allen Browne wrote:
Parse the color into bytes for red, green, and blue, using integer division
and Mod.


HI Allen - thanks to you and Lyle for your responses. For posterity,
here's the function I based on your submission (Lyle's seemed to work,
but for the very limited amount of testing I did with it, I kept getting
black colours).

I tried parsing the colour into integers since the RGB() function
requires integer arguments, but I ended up with vastly different looking
colours which were mostly black. I don't understand why using bytes
worked better, but <shrug>
Public Function fReverseColour(lngColour As Long) As Long

'Takes an RGB colour spec and reverses it.
'Thanks to Allen Browne & Lyle Fairfield's responses on cdma
'
'Argument LngColour is the colour code fed from a colour
'property of a control's BackColor (the usual one used
'in TCM), foreColor, etc.

Dim bytRed As Byte, bytGreen As Byte, bytBlue As Byte

Dim bytTemp As Byte

'Before using the above bytRed, etc bytes,
'use bytTemp for a calculation because for some
'lngColour codes such as the standard Access default
'form BG colour, the results of some of the division and
'mod functions is greater than 255. See error handling

Dim lngC As Long

On Error GoTo Err_Proc

bytTemp = lngColour Mod 256

bytRed = bytTemp

lngC = lngColour \ 256

bytTemp = lngC Mod 256

bytGreen = bytTemp

bytTemp = lngC \ 256

bytBlue = bytTemp

bytRed = Not bytRed

bytGreen = Not bytGreen

bytBlue = Not bytBlue

fReverseColour = RGB(bytRed, bytGreen, bytBlue)

Debug.Print "Original Colour Fed: " & lngColour & _
" Reverse: " & fReverseColour

Exit_Proc:

Exit Function

Err_Proc:

Select Case Err.Number

Case 6 'overflow on bytTemp calc

bytTemp = 255

Resume Next

Case Else

MsgBox "Error " & Err.Number & " " & _
Err.Description, vbCritical, _
"Function fReverseColour", Err.HelpFile _
, Err.HelpContext

Resume Exit_Proc

End Select

End Function

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Dec 28 '05 #4

P: n/a
"Tim Marshall" <TI****@PurplePandaChasers.Moertherium> wrote in message
news:do**********@coranto.ucs.mun.ca...
Allen Browne wrote:
Parse the color into bytes for red, green, and blue, using integer division and Mod.


HI Allen - thanks to you and Lyle for your responses. For posterity,
here's the function I based on your submission (Lyle's seemed to work,
but for the very limited amount of testing I did with it, I kept getting
black colours).

I tried parsing the colour into integers since the RGB() function
requires integer arguments, but I ended up with vastly different looking
colours which were mostly black. I don't understand why using bytes
worked better, but <shrug>
Public Function fReverseColour(lngColour As Long) As Long

'Takes an RGB colour spec and reverses it.
'Thanks to Allen Browne & Lyle Fairfield's responses on cdma
'
'Argument LngColour is the colour code fed from a colour
'property of a control's BackColor (the usual one used
'in TCM), foreColor, etc.

Dim bytRed As Byte, bytGreen As Byte, bytBlue As Byte

Dim bytTemp As Byte

'Before using the above bytRed, etc bytes,
'use bytTemp for a calculation because for some
'lngColour codes such as the standard Access default
'form BG colour, the results of some of the division and
'mod functions is greater than 255. See error handling

Dim lngC As Long

On Error GoTo Err_Proc

bytTemp = lngColour Mod 256

bytRed = bytTemp

lngC = lngColour \ 256

bytTemp = lngC Mod 256

bytGreen = bytTemp

bytTemp = lngC \ 256

bytBlue = bytTemp

bytRed = Not bytRed

bytGreen = Not bytGreen

bytBlue = Not bytBlue

fReverseColour = RGB(bytRed, bytGreen, bytBlue)

Debug.Print "Original Colour Fed: " & lngColour & _
" Reverse: " & fReverseColour

Exit_Proc:

Exit Function

Err_Proc:

Select Case Err.Number

Case 6 'overflow on bytTemp calc

bytTemp = 255

Resume Next

Case Else

MsgBox "Error " & Err.Number & " " & _
Err.Description, vbCritical, _
"Function fReverseColour", Err.HelpFile _
, Err.HelpContext

Resume Exit_Proc

End Select

End Function

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto


Tim, I think this will work to compute "complementary" colors:

NewColor = &HFFFFFF Xor OldColor

?vbred
255
?vbgreen
65280
?vbblue
16711680
?&HFFFFFF xor vbred
16776960
?vbgreen + vbblue
16776960
?&HFFFFFF xor vbblue
65535
?vbgreen + vbred
65535
?&HFFFFFF xor (vbblue + vbgreen)
255

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.

Dec 28 '05 #5

P: n/a
Tim

Have you thought about SystemColors? Many default colors in Access are
System Colors. (They are the long negative numbers.) Unlike RGB colors
they use the fourth byte and reversing them by manipulating only the
RGB bytes generally returns black.

I offer this for anyone (you included of course) who wants to work with
System Colors as well as RGB Colors.

Const MinimumSystemColor As Long = &H80000001
Const MaximumSystemColor As Long = &H80000018

Private Declare Function OleTranslateColor Lib "oleaut32" _
(ByVal SystemColorConstants As Long, _
ByVal Pallette As Long, _
RGBColor As Long) As Long

Public Function ReverseColor(ByVal Color As Long) As Long
If Color >= MinimumSystemColor And Color <= MaximumSystemColor Then
OleTranslateColor Color, 0, Color
End If
ReverseColor = Not Color And vbWhite
End Function

Dec 28 '05 #6

P: n/a
Lyle Fairfield wrote:
Tim

Have you thought about SystemColors? Many default colors in Access are
System Colors. (They are the long negative numbers.) Unlike RGB colors
they use the fourth byte and reversing them by manipulating only the
RGB bytes generally returns black.
THanks. Hmmmmmm. Actually, to be honest, my function tends to usually
return black. For the most part, the lngColour argument fed into it are
custom colours made by the user previously using the colour picker (The
Kreft/Lebans utility I mentioned in my original post). So they are not
usually negative (thanks very much for that explanation, BTW, I've
always wondered why a few colours were negative - the fourth byte might
actually why my function had to have an error handler for error 6,
overflow - I think).

I must ask a couple of questions:
I offer this for anyone (you included of course) who wants to work with
System Colors as well as RGB Colors.

Const MinimumSystemColor As Long = &H80000001
Const MaximumSystemColor As Long = &H80000018
The values you've used here for min/max system colours - what do they
mean? This looks a bit like the sort of colouring coding I've done in
my rather basic html work.

How do you know &H80000001 and &H80000018 are the min/max system
colours? Is this a windows property?
Private Declare Function OleTranslateColor Lib "oleaut32" _
(ByVal SystemColorConstants As Long, _
ByVal Pallette As Long, _
RGBColor As Long) As Long

Public Function ReverseColor(ByVal Color As Long) As Long
If Color >= MinimumSystemColor And Color <= MaximumSystemColor Then
OleTranslateColor Color, 0, Color
End If
ReverseColor = Not Color And vbWhite
End Function


I appreciate your responses on this, thanks.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Dec 29 '05 #7

P: n/a
Tim Marshall wrote:
THanks. Hmmmmmm. Actually, to be honest, my function tends to usually
return black.


YOu may, in this case, call me a stupid backwoods newfie (and I won't go
ballistic like I usually do when I hear our equivalent of the "n"
word)... the reason things were returning black was simply because the
code I had which called my fReverseColour function applied the resultant
colour to the backcolor instead of the forecolor property... With
backstyle being transparant, I was seeinbg no effect of my function
whatsoever, though it had seemed to work well in the debug window as far
as different colours go.

&*^^&*% What a moron.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Dec 29 '05 #8

P: n/a
I haven't found a great pgae that describes min and max system colors.
To get my min and max values I just looked at the Object Browser. I
missed &H80000000 so my range should be adjusted:
Const MinimumSystemColor As Long = &H80000000

Dec 29 '05 #9

P: n/a
We all figure out all the tough parts and then doing something not so
clever with the result quite regularly.

Dec 29 '05 #10

P: n/a
Another try at this which doesn't rely in a range of system colors but
on the return of the API function:

Option Explicit

Private Const SUCCESS_SUCCESS = 0

Private Declare Function OleTranslateColor Lib "oleaut32" _
(ByVal SystemColorConstants As Long, _
ByVal Pallette As Long, _
RGBColor As Long) As Long

Public Function TranslateColor(ByVal color As Long) As Long
If OleTranslateColor(color, 0, TranslateColor) <> SUCCESS_SUCCESS
Then TranslateColor = color
End Function

Public Function ReverseColor(ByVal color As Long) As Long
ReverseColor = Not TranslateColor(color) And vbWhite
End Function

Dec 29 '05 #11

P: n/a
Here is an older post of mine on this subject:
From: Stephen Lebans (NoEm...@please.com)
Subject: Re: Windows color values
Newsgroups: microsoft.public.access.formscoding
View: Complete Thread (4 articles) | Original Format
Date: 2002-03-10 11:15:14 PST
Peter here is an older post of mine on this subject.
For information on Window's System Color Indexes see:
http://support.microsoft.com/support...Q186/0/62.ASP?
From: Stephen Lebans (NoEm...@please.com)
Subject: Re: Complete color chart
Newsgroups: microsoft.public.access.forms,
microsoft.public.access.formscoding,
microsoft.public.access.gettingstarted, microsoft.public.access.reports
View: Complete Thread (9 articles) | Original Format
Date: 2001-11-10 17:23:48 PST
Tom I asked the poster to open the Windows Color Dialog window and play
around with the RGB color selection process to understand how colors are
represented.
To understand how to seperate a long RGB Color value back to its RGB
components see:
http://support.microsoft.com/support.../Q112/0/61.asp
For a detailed online Color FAQ see:
http://www.inforamp.net/~poynton/Poynton-color.html
Specifically:
http://www.inforamp.net/~poynton/ColorFAQ.html
For information on how windows represents colors see:
http://msdn.microsoft.com/library/de.../en-us/gdi/col
ors_87zn.asp
In case you do not have access to MSDN:
Working with Color
See Also
Visual Basic uses a consistent system for all color properties and
graphics methods. A color is represented by a Long integer, and this
value has the same meaning in all contexts that specify a color.
Specifying Colors at Run Time
There are four ways to specify a color value at run time:
Use the RGB function.
Use the QBColor function to choose one of 16 Microsoft QuickBasic®
colors.
Use one of the intrinsic constants listed in the Object Browser.
Enter a color value directly.
This section discusses how to use the RGB and QBColor functions as
simple ways to specify color. See "Using Color Properties" later in this
chapter for information on using constants to define color or directly
entering color values.
Using the RGB Function
You can use the RGB function to specify any color.
To use the RGB function to specify a color
Assign each of the three primary colors (red, green, and blue) a number
from 0 to 255, with 0 denoting the least intensity and 255 the greatest.
Give these three numbers as input to the RGB function, using the order
red-green-blue.
Assign the result to the color property or color argument.
Every visible color can be produced by combining one or more of the
three primary colors. For example:
' Set background to green.
Form1.BackColor = RGB(0, 128, 0)
' Set background to yellow.
Form2.BackColor = RGB(255, 255, 0)
' Set point to dark blue.
PSet (100, 100), RGB(0, 0, 64)
For More Information For information on the RGB function, see "RGB
Function" in the Language Reference.
Using Color Properties
See Also
Many of the controls in Visual Basic have properties that determine the
colors used to display the control. Keep in mind that some of these
properties also apply to controls that aren't graphical. The following
table describes the color properties.
Property Description
BackColor Sets the background color of the form or control used for
drawing. If you change the BackColor property after using graphics
methods to draw, the graphics are erased by the new background color.
ForeColor Sets the color used by graphics methods to create text or
graphics in a form or control. Changing ForeColor does not affect text
or graphics already created.
BorderColor Sets the color of the border of a shape control.
FillColor Sets the color that fills circles created with the Circle
method and boxes created with the Line method.
For More Information For detailed descriptions of these color
properties, see "BackColor Property," "ForeColor Property," "BorderColor
Property," and "FillColor Property" in the Language Reference.
Defining Colors
The color properties can use any of several methods to define the color
value. The RGB function described in "Working with Color" is one way to
define colors. This section discusses two more ways to define colors:
Using defined constants
Using direct color settings
Using Defined Constants
You don't need to understand how color values are generated if you use
the intrinsic constants listed in the Object Browser. In addition,
intrinsic constants do not need to be declared. For example, you can use
the constant vbRed whenever you want to specify red as a color argument
or color property setting:
BackColor = vbRed
Using Direct Color Settings
Using the RGB function or the intrinsic constants to define color are in
direct methods. They are indirect because Visual Basic interprets them
into the single approach it uses to represent color. If you understand
how colors are represented in Visual Basic, you can assign numbers to
color properties and arguments that specify color directly. In most
cases, it's much easier to enter these numbers in hexadecimal.
The valid range for a normal RGB color is 0 to 16,777,215 (&HFFFFFF&).
Each color setting (property or argument) is a 4-byte integer. The high
byte of a number in this range equals 0. The lower 3 bytes, from least
to most significant byte, determine the amount of red, green, and blue,
respectively. The red, green, and blue components are each represented
by a number between 0 and 255 (&HFF).
Consequently, you can specify a color as a hexadecimal number using this
syntax:
&HBBGGRR&
The BB specifies the amount of blue, GG the amount of green, and RR the
amount of red. Each of these fragments is a two-digit hexadecimal number
from 00 to FF. The median value is 80. Thus, the following number
specifies gray, which has the median amount of all three colors:
&H808080&
Setting the most significant bit to 1 changes the meaning of the color
value: It no longer represents an RGB color, but an environment-wide
color specified through the Windows Control Panel. The values that
correspond to these system-wide colors range from &H80000000 to
&H80000015.
Note Although you can specify over 16 million different colors, not
all systems are capable of displaying them accurately. For more
information on how Windows represents colors, see "Working with 256
Colors" later in this chapter.
Using System Colors
When setting the colors of controls or forms in your application, you
can use colors specified by the operating system instead of specific
color values. If you specify system colors, when users of your
application change the values of system colors on their computers, your
application automatically reflects the user-specified color values.
Each system color has both a defined constant and a direct color
setting. The high byte of direct color settings for system colors
differs from those of normal RGB colors. For RGB colors, the high byte
equals 0 whereas for system colors the high byte equals 8. The rest of
the number refers to a particular system color. For example, the
hexadecimal number used to represent the color of an active window
caption is &H80000002&.
When you select color properties at design time with the Properties
window, selecting the System tab lets you choose system settings, which
are automatically converted into the hexadecimal value. You can also
find the defined constants for system colors in the Object Browser.
Constant Value Description
vbScrollBars 0x80000000 Scroll bar color
vbDesktop 0x80000001 Desktop color
vbActiveTitleBar 0x80000002 Color of the title bar for the active window
vbInactiveTitleBar 0x80000003 Color of the title bar for the inactive
window
vbMenuBar 0x80000004 Menu background color
vbWindowBackground 0x80000005 Window background color
vbWindowFrame 0x80000006 Window frame color
vbMenuText 0x80000007 Color of text on menus
vbWindowText 0x80000008 Color of text in windows
vbTitleBarText 0x80000009 Color of text in caption, size box, and scroll
arrow
vbActiveBorder 0x8000000A Border color of active window
vbInactiveBorder 0x8000000B Border color of inactive window
vbApplicationWorkspace 0x8000000C Background color of multiple-document
interface (MDI) applications
vbHighlight 0x8000000D Background color of items selected in a control
vbHighlightText 0x8000000E Text color of items selected in a control
vbButtonFace 0x8000000F Color of shading on the face of command buttons
vbButtonShadow 0x80000010 Color of shading on the edge of command
buttons
vbGrayText 0x80000011 Grayed (disabled) text
vbButtonText 0x80000012 Text color on push buttons
vbInactiveCaptionText 0x80000013 Color of text in an inactive caption
vb3DHighlight 0x80000014 Highlight color for 3-D display elements
vb3DDKShadow 0x80000015 Darkest shadow color for 3-D display elements
vb3DLight 0x80000016 Second lightest 3-D color after vb3DHighlight
vbInfoText 0x80000017 Color of text in ToolTips
vbInfoBackground 0x80000018 Background color of ToolTips

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"Tim Marshall" <TI****@PurplePandaChasers.Moertherium> wrote in message
news:do**********@coranto.ucs.mun.ca...
Lyle Fairfield wrote:
Tim

Have you thought about SystemColors? Many default colors in Access are
System Colors. (They are the long negative numbers.) Unlike RGB colors
they use the fourth byte and reversing them by manipulating only the
RGB bytes generally returns black.


THanks. Hmmmmmm. Actually, to be honest, my function tends to usually
return black. For the most part, the lngColour argument fed into it are
custom colours made by the user previously using the colour picker (The
Kreft/Lebans utility I mentioned in my original post). So they are not
usually negative (thanks very much for that explanation, BTW, I've always
wondered why a few colours were negative - the fourth byte might actually
why my function had to have an error handler for error 6, overflow - I
think).

I must ask a couple of questions:
I offer this for anyone (you included of course) who wants to work with
System Colors as well as RGB Colors.

Const MinimumSystemColor As Long = &H80000001
Const MaximumSystemColor As Long = &H80000018


The values you've used here for min/max system colours - what do they
mean? This looks a bit like the sort of colouring coding I've done in my
rather basic html work.

How do you know &H80000001 and &H80000018 are the min/max system colours?
Is this a windows property?
Private Declare Function OleTranslateColor Lib "oleaut32" _
(ByVal SystemColorConstants As Long, _
ByVal Pallette As Long, _
RGBColor As Long) As Long

Public Function ReverseColor(ByVal Color As Long) As Long
If Color >= MinimumSystemColor And Color <= MaximumSystemColor Then
OleTranslateColor Color, 0, Color
End If
ReverseColor = Not Color And vbWhite
End Function


I appreciate your responses on this, thanks.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto

Dec 29 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.