Solved Adjust Luminosity via VBA of any color value to it's lightest shade (1 Viewer)

Mike Krailo

Well-known member
Local time
Today, 03:28
Joined
Mar 28, 2020
Messages
1,030
My current project requires some user defined colors to be entered into the form. The form allows for the generation of custom reports. These colors adjust a Reports title font and some other controls as well. The problem that I need to solve is how to take any of those colors entered and calculate the lightest shade that can be produced for that given color. This calculated value will be used to set the back color of the RptTitle control on the report based on the entered color for the Border of that same control. This allows the user to enter just two base colors to which other shades are calculated and set on other controls.

Hope that makes sense.

The colors are entered via a text box with a value like "#F67912". I have a function that converts that to the Decimal value of 1210870. Now I want another function that takes that value 1210870 and converts it to 16251903 which is close to the lightest shade of that original color. What's the best way to go about this?

Here is my function for doing the basic hex to decimal conversion:

Code:
Public Function HexToDec(HexCode As String) As Long
   Dim Red As String
   Dim Green As String
   Dim Blue As String
   Dim NewHex As String
   NewHex = Right(HexCode, 6) 'strips the octothorp if there is one
   Red = Left(NewHex, 2)
   Green = Mid(NewHex, 3, 2)
   Blue = Right(NewHex, 2)
   HexToDec = RGB("&H" & Red, "&H" & Green, "&H" & Blue)
End Function

Update: Just changed the title from HUE to Luminosity since that is more what I really meant.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:28
Joined
May 7, 2009
Messages
19,169
better to use Table with r,g,b values.
you add another field (to group each colors).
another field to signify their shades, 1 being the lightest.

there is a sample rgb chart here:
RGB Color Codes Chart 🎨 (rapidtables.com)
 

Mike Krailo

Well-known member
Local time
Today, 03:28
Joined
Mar 28, 2020
Messages
1,030
The users want to use HEX values as the input into the form. So are you saying to convert the HEX to RGB values instead of decimal and compare to a table with ALL the colors defined already?

Ideally I would like something like:

GetLighterShade("#F67912", 1) ' where 1 is the lightest shade possible without being completely white)

The result could be in decimal or hex as long as I can use the value to set the properties accordingly. This would be easy if I could look at the code for that HUE slider on the color picker. That's what I want in VBA is just to adjust any color to a H value of 1. But there is no H value in any of the functions, just RGB.

That site has a very nice break down of the colors and all the codes. It's still not clear how to derive the lightest shade if given any RGB value or HEX value. I thought maybe it would be as simple as adding 1 to each RGB value until one of them reached FF and that would end up being the lightest shade of the original color. I'm going to have to experiment I guess.

This is a pick of color cop dialog for custom colors, notice that LUM parameter. That's what I want in VBA. If I set it to 230 on any color it will be the lightest shade of any color or close to it.

1616858857345.png
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 07:28
Joined
Jan 14, 2017
Messages
18,186
See if this app helps

1616859599853.png
 
Last edited:

Minty

AWF VIP
Local time
Today, 07:28
Joined
Jul 26, 2013
Messages
10,355
I looked into something similar to this a while ago. I wanted to set a load of coloured Powerpoint colour values to 50% transparency of another slide item. You would think that you could use the very handy and obvious transparency property, but alas no, in Microsoft's infinite wisdom that buggers messes up any other formatting applied to the shape and randomly sets the colour to some other attribute.

Oh well, back to the drawing board - I'll simply work out what colour value is roughly 50% and use that.
I never managed to work out how to do it.

If someone knows of a method I'm all ears.
 

Mike Krailo

Well-known member
Local time
Today, 03:28
Joined
Mar 28, 2020
Messages
1,030
I was hoping someone already hashed out the solution already but it looks like I'll be yanking out the VBA Handbook again. There has to be an algorithm for doing this to the RGB values or using that formula for the decimal value to simply change luminosity or in Minty's case the transparency. That slider in the pic I posted does exactly what I want so there must be a way to do it in VBA.

Having a solution to this would open the doors to creating theme color sets based on just two or three main colors. All the rest of the colors get set based on different levels of luminosity or shades. I'll keep plugging away at it.
 

isladogs

MVP / VIP
Local time
Today, 07:28
Joined
Jan 14, 2017
Messages
18,186
It would appear you didn't look at the link to my colour converter app.
Amongst the many conversion functions included are HLS to RGB and vice versa.
Look at the module modColourConvert.

It also contains code to manage the Colour selector dialog as shown in post #3
 

Mike Krailo

Well-known member
Local time
Today, 03:28
Joined
Mar 28, 2020
Messages
1,030
I must have missed that Collin, I did look at it and did not see anything related to the HLS assuming that is Hue, Luminosity, and Saturation. If you do have that in there, I must be blind because the parameters I seen in the app did not include the HLS values. Maybe I need new glasses. If it is in there, your gonna be my hero.

After putting on a new pair of glasses, yes Collin, you do have code in there that I need to investigate further. Thank you for pointing that out. And you get a like for that.
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 07:28
Joined
Jan 14, 2017
Messages
18,186
Hope it helps. I added the HLS code years ago but never actually used it as I had no need for it.
Hope it makes sense. I've left the links to where it came from
 

Mike Krailo

Well-known member
Local time
Today, 03:28
Joined
Mar 28, 2020
Messages
1,030
Yes, leaving the links was great. I already checked it out.
 

isladogs

MVP / VIP
Local time
Today, 07:28
Joined
Jan 14, 2017
Messages
18,186
And....? Does it look like you have a solution?
 

Mike Krailo

Well-known member
Local time
Today, 03:28
Joined
Mar 28, 2020
Messages
1,030
Collin, I had to go to work for several hours so that's why I didn't respond right away. I looked at the functions in your module and don't really understand how they work. There is an RGBtoHLS function that takes 6 arguments. I would think going from rgb there would be three. Not sure what to do with it.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 07:28
Joined
Sep 12, 2006
Messages
15,614
out of interest, given that rgb are each in the region of 0 to 255, would you achieve what you want by moving each colour half way to 255
(or a simple variation of this idea - I don't know whether the hues graduate in a linear fashion)

so given 60,90,120 you move to (60+255)/2, (90+255)/2,(120+255)/2 = 157,172,187

in this scenario 0,0,0 (black) becomes 127,127,127 which I presume is a mid grey
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:28
Joined
Feb 28, 2001
Messages
27,001
The issue is that as long as you are working RGB that things don't scale linearly. One of the colors I used was an ORANGE but when you try to proportionally darken it, it becomes a muddy brown and if you try to brighten it, the darn thing becomes yellow. I solved the problem another way by having a table of presets that would allow you to choose any one of five different shadings of the same same color. Program-wise, that was easier than trying to jiggle virtual sliders to darken or lighten the hue.
 

Mike Krailo

Well-known member
Local time
Today, 03:28
Joined
Mar 28, 2020
Messages
1,030
So we are back to Arnel's answer in post #2 since he suggested something similar.

I may end up doing the same Doc. Right now I only need the real light shades of any given color but thought there might be a know algorithm for doing any shade possible. You are correct that it is not linear but the slider is. And that is the mystery here how that slider function works.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:28
Joined
Feb 28, 2001
Messages
27,001
The problem is in our eyes. As it happens, you can ramp up/down the individual color elements by determining the percent of the total light intensity you have for each color. Then when you ramp it up or down, you just reallocate the bits. EXCEPT - that like ALL human senses, our visual responses are not linear - they are logarithmic. The RGB intensities ARE linear - but our eyes aren't.
 

Mike Krailo

Well-known member
Local time
Today, 03:28
Joined
Mar 28, 2020
Messages
1,030
I did find this site this morning: Color Shades Generator
That is really nice but how did they do it? Java Script most likely. Look at the bottom of the page and there is a bunch of other related tools for manipulating colors.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 07:28
Joined
Sep 12, 2006
Messages
15,614
I did find this site this morning: Color Shades Generator
That is really nice but how did they do it? Java Script most likely. Look at the bottom of the page and there is a bunch of other related tools for manipulating colors.

Interesting resource.

It's not obvious how the rgb values vary from shade to shade. There must be a simple formula to move from a deep pure red rgb(26, 0, 0) through to a middle pure red (but not full saturation red) rgb(230, 0, 0) - (note that 230+26 = 256), and then bring in gradually increasing amounts of GB to lighten the red in degrees of pinkness.

what are the HSL and HSV? Are they separate values, or are they functions of the RGB number?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:28
Joined
Feb 28, 2001
Messages
27,001
HSL is Hue, Saturation, Luminosity - the other main way to express color besides RGB.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 07:28
Joined
Sep 12, 2006
Messages
15,614
HSL is Hue, Saturation, Luminosity - the other main way to express color besides RGB.
Yes - but is that the same as RGB, just expressed another way - or something different?
Is transparency the same? - is that just a by product of RGB, or is that actually something that changes the pixel colours based on the pixel that was underneath?
 

Users who are viewing this thread

Top Bottom