Solved

How to make a color lighter or darker

Posted on 2004-04-16
10
2,431 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 50

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 50

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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.
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.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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