Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How to make a color lighter or darker

Posted on 2004-04-16
Medium Priority
2,668 Views
Hi,
First excuse my bad english :-)
Given a color (hexadecimal value), I know how to extract its 3 components :
Color = &Hxxxxxxxx
R = Color Mod &H100
Color = Color \ &H100
G = Color Mod &H100
Color = Color \ &H100
B = Color Mod &H100
From that parameters, I want to get a new color being 10% lighter (or darker) ...
If I only write something like R = R*110/100 ... and so on for G and B, it doesn't work.
How can I do ?
Philippe.
0
Question by:MSelect
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 2
• 2

LVL 76

Expert Comment

ID: 10844498
Hopefully you are recombining the components so
Color = R + G * 256 + B * 256 ^ 2.
You should ensure that each component doesn't exceed 255 (&HFF)
Also, watch out if you are trying to change a component colour. It's probably not a colour, but a reference to a system colour and will come out negative.
0

LVL 53

Expert Comment

ID: 10845156
Try:

Public Declare Function TranslateColor Lib "olepro32.dll" Alias "OleTranslateColor" (ByVal clr As OLE_COLOR, ByVal palet As Long, col As Long) As Long

'Get RGB Color value Into Array From An OLEColor
Public Sub GetRGBColorInArray(ByRef OLEColor As Long, ByRef iArr() As Long)
Dim RealColor As Long
Erase iArr
'Convert OLE colors to RGB colors
TranslateColor OLEColor, 0, RealColor
ReDim iArr(2)
iArr(0) = RealColor And &HFF&
iArr(1) = (RealColor And &HFF00&) / 2 ^ 8
iArr(2) = (RealColor And &HFF0000) / 2 ^ 16
End Sub

then..

Dim myArr() As Long
'GetColor myArr
GetRGBColorInArray Me.BackColor, myArr
MsgBox "(Array Value in Form) R: " & myArr(0) & " G: " & myArr(1) & " B: " & myArr(2)

Hope this helps
0

LVL 76

Expert Comment

ID: 10845158
I have just noticed that I have used the word component(s) three times, but with two different meanings. The first two times I was referring to the primary colour component of the overall colour.
The third time I was referring to a VB component (Control). Sorry to confuse you, especially as you have apologised (unecessarily) for your English.
0

LVL 76

Expert Comment

ID: 10845213
Ryancys, surely a young man like you should be in bed. My own excuse is that I'm not young (just retired) and it's only 20:06 here. Greenwich is only a few miles way, so my watch still says 19:06 which is GMT, in spite of what the government saying it's now Summer Time.
0

LVL 53

Expert Comment

ID: 10845290
Actually it's 3:10 am at my local time (SGT - +0800), used to it, as my previous job (i'm currently temporary unemployed) forced me to come back home very late at the time, in additional, i'm searching some stuffs on .Net material for my "internal" application, that's why i'm still here.. ;-)
0

LVL 76

Expert Comment

ID: 10845705
ryancys,
Is "SGT" Singapore General Time?
I'm going to watch TV now. I hope for your sake that you're going to bed.
Graham

Mselect,
Sorry for the private conversation, but at least you know we're still here and watching your question.
Graham
0

Author Comment

ID: 10846041
Thanks but ... It would seem that it doesn't help me ... you must assume that I'm not a professionnal programmer :-)

First, it is not a system color and, therefore, if I well understand your explanations, the TranslateColor function has nothing to do with my problem.

Then, when I have got R, G and B from my initial color, by using my own function that well works, what does exactly do your following suggestion
Color = R + G * 256 + B * 256 ^ 2     ???
By using it, one always gets a new color, "strange", especially with a red unwanted component.
How can we set the darkness/brightness to +10% or -20% or +30% or ...

Please do explain ... but for tomorrow because it's 11:00 pm here and time to join Morpheus too :-)
0

LVL 76

Expert Comment

ID: 10848214
Hi MSelect,
To use the new colour, you must recombine the parts, so

Color = R + G * 256 + B * 256 ^ 2

makes a new Long number for your new colour. I've used decimal rather than hex arithmetic.

This full procedure works on my system

Private Sub Command1_Click()
Dim R As Long
Dim G As Long
Dim B As Long
Dim Color As Long

'Set Form colour
Form1.BackColor = &H808080
Color = Form1.BackColor

'Break the colour into its primary parts
R = Color Mod &H100
Color = Color \ &H100
G = Color Mod &H100
Color = Color \ &H100
B = Color Mod &H100

'Modify the red part and ensure that it's not over the maximum value
R = R * 11 / 10
If R > 255 Then
R = 255
End If

'Recombine and make the form a slightly pink colour
Form1.BackColor = R + G * 256 + B * 256 ^ 2
End Sub

0

LVL 76

Accepted Solution

GrahamSkan earned 1000 total points
ID: 10848227
To make it brighter without changing the hue you shouls cahnge the other two primary colours:

'Modify the red part and ensure that it's not over the maximum value
R = R * 11 / 10
If R > 255 Then
R = 255
End If
'Modify the green part and ensure that it's not over the maximum value
G = G * 11 / 10
If G > 255 Then
G = 255
End If
'Modify the Blue part and ensure that it's not over the maximum value
B = B * 11 / 10
If B > 255 Then
B = 255
End If

'Recombine and make the form slightly brighter
Form1.BackColor = R + G * 256 + B * 256 ^ 2
End Sub

0

Author Comment

ID: 10849424
Thank you very much for your effort.
Philippe.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for â€˜enumerationsâ€™) are not often used by programmers but they can be quite valuable when they are. Â What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that containsâ€¦
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). Uâ€¦
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
###### Suggested Courses
Course of the Month5 days, 19 hours left to enroll