Solved

How to make a color lighter or darker

Posted on 2004-04-16
10
2,365 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:MSelect
  • 6
  • 2
  • 2
10 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 10844498
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
 
LVL 49

Expert Comment

by:Ryan Chong
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

by:GrahamSkan
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

by:GrahamSkan
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 49

Expert Comment

by:Ryan Chong
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 76

Expert Comment

by:GrahamSkan
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

by:MSelect
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

by:GrahamSkan
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

by:
GrahamSkan earned 250 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

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now