Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to make a color lighter or darker

Posted on 2004-04-16
10
Medium Priority
?
2,724 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 54

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 54

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

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

886 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