473,763 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reversing a COlour Code

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
11 8093
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****@PurpleP andaChasers.Moe rtherium> 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
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
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
"Tim Marshall" <TI****@PurpleP andaChasers.Moe rtherium> 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 "complement ary" 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
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 MinimumSystemCo lor As Long = &H80000001
Const MaximumSystemCo lor As Long = &H80000018

Private Declare Function OleTranslateCol or Lib "oleaut32" _
(ByVal SystemColorCons tants As Long, _
ByVal Pallette As Long, _
RGBColor As Long) As Long

Public Function ReverseColor(By Val Color As Long) As Long
If Color >= MinimumSystemCo lor And Color <= MaximumSystemCo lor Then
OleTranslateCol or Color, 0, Color
End If
ReverseColor = Not Color And vbWhite
End Function

Dec 28 '05 #6
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 MinimumSystemCo lor As Long = &H80000001
Const MaximumSystemCo lor 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 OleTranslateCol or Lib "oleaut32" _
(ByVal SystemColorCons tants As Long, _
ByVal Pallette As Long, _
RGBColor As Long) As Long

Public Function ReverseColor(By Val Color As Long) As Long
If Color >= MinimumSystemCo lor And Color <= MaximumSystemCo lor Then
OleTranslateCol or 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
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
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 MinimumSystemCo lor As Long = &H80000000

Dec 29 '05 #9
We all figure out all the tough parts and then doing something not so
clever with the result quite regularly.

Dec 29 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5973
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart in how I can accomplish this. Also, I don't know the the size of the array before hand as the image can be any size. I already have the my read and write gif functions working, but I just need to know how to reverse the contents.
2
2493
by: Lois | last post by:
My website formatting knowledge is mostly HTML; I know hardly any Javascript. Recently I created a site with a nav bar in table format across the top of the page, and I made the background colour of the cell with the name of the page that the reader was on a different shade, like this: Home | This | That | Other | Etc. If you were on "Other" page, for example, the background colour of the "Other" cell was a different shade from the...
6
5236
by: Louise | last post by:
Hi I have written an HTML pages which does not have any colour specifying tags as far I know. When I view this in an Microsoft internet explorer browser it appears with a white background and black text but when I change Windows start menu->settings->control panel ->display -> appearance and change scheme to 'High Contrast Black' the background in the browser changes to black and the text to white. I understand that the windows scheme...
6
2797
by: Lapchien | last post by:
One of my users has asked that a form change it's colour if a particular yes/no box is ticked - possibly made a bit more tricky because the form is tabular..? Thanks, Lap
4
1450
by: Thief_ | last post by:
I'm trying to create many squares on a form and for every new square, change the background colour to the next colour. I'm trying to create a colour palette showing all available colours and their colour name. My problem is that I can not figger out how to create a new panel at specific intervals across and down the form. Here's the code I have so far: Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
45
5221
by: Ajay | last post by:
Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed.
20
3167
by: Chor Lit | last post by:
Hi, I asked Bjarne Stroustrup about the idea of adding colour standard for C++, and he said that it is very difficult for compiler vendors to change their IDE. But do you think it is possible ? Note that the proposed colour standard is not just merely to ease the eye only as what presently is in C++ compilers, but to aid in syntax disambiguation and other advantages. Here are a few advantages that I can think of:
0
1596
by: Badino | last post by:
Hi, Can someone tell me what to put in this code so that if a user selects 0 (Black) then make the font white (0) as my default colour is black on my Excel spreadsheet. Private Type CHOOSECOLOUR lStructSize As Long hwndOwner As Long Hinstance As Long rgbResult As Long lpCustColors As String
6
2709
Robbie
by: Robbie | last post by:
Hi. I've made 2 functions which play around with colours. They convert a 'colour number' (I don't know what the proper name for it is, so I call it this - the Long given back by RGB(), object.BackColor, etc, which represents a value of red, green and blue) into another 'colour number' but this is a greyscale version of it. There are 2 functions - the first one 'decodes' the 'colour number' into the separate R G and B values. The second one...
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.