# How to make a color lighter or darker

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 ?
Thank you for your help.
Philippe.
Asked:
###### Who is Participating?

RetiredCommented:
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

RetiredCommented:
That looks about right.
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

Commented:
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

RetiredCommented:
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

RetiredCommented:
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

Commented:
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

RetiredCommented:
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 Commented:
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

RetiredCommented:
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

Author Commented:
Thank you very much for your effort.
Philippe.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Already a member? Login.

All Courses

From novice to tech pro — start learning today.