Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel Luminance value 3

Status
Not open for further replies.

samji3877

Technical User
Feb 27, 2011
5
GB
I'm trying to create a simple model of the eye retina in excel, I have a 10x10 grid in sheet 1 with different shades of black/grey/white background

on sheet 2 I again have a 10x10 grid and I want each cell to provide a numerical value of the luminance of the corresponding cell in sheet 1.

Can this be done and how?
 
Hey mscallisto,

cheers for that but unfortunately it doesn't help, I don't require a custom colour, what I need is a formula that allows a cell to identify the luminance of another cell in a numerical form.

Cheers for your help

Sam
 
Sorry - posted that a bit quickly. A UDF is a User-Defined Function and it means a custom function of some sort, using VBA, which you have to write.

So if you want to put a formula in a cell that looks something like "[green]=LUMINANCE(A1)[/green]", then LUMINANCE is not a built-in function so you have to write it - hence my pointing you to code, earlier.

If that is what you do want - and I wasn't entirely sure from your original post - and you need help putting the pieces together, please come back.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
yes please (I'd seen that it required a user function but from there I was stumped)

Cheers TonyJollans

Sam
 
I will be interested to see how this is accomplished but Sam, you should re-post in forum707 since the solution requires VBA.

Gavin
 
Hi Sam,

Firstly, my apologies for the time it has taken me to get back - I couldn't get on-line long enough yesterday to post.

Gavin is correct, so it would probably be best if you have any non-trivial questions about this code, that they should be posted in the VBA forum. For completeness, however, I will post the code here.

There are things you could do for the specific case that you have of wanting the luminance of a specific cell - the corresponding cell in another sheet - but a basic function can be built very easily from the code on my site. The HSL Type definition and the RGBToHSL function are just a cut and paste job; all that is extra is the Luminance function to drive the thing. Put this in a module in your workbook:

Code:
Option Explicit

Type HSL
    H As Double ' Range 0 — 1
    S As Double ' Range 0 — 1
    L As Double ' Range 0 — 1
End Type

Function Luminance(Cell As Excel.Range) As Long
 
    Application.Volatile
    Luminance = Round(RGBtoHSL(Cell.Interior.Color).L * 255)
    
End Function

Function RGBtoHSL(RGB As Long) As HSL

    Dim R As Double ' Range 0 — 1
    Dim G As Double ' Range 0 — 1
    Dim B As Double ' Range 0 — 1

    Dim RGB_Max  As Double
    Dim RGB_Min  As Double
    Dim RGB_Diff As Double

    Dim HexString As String

    HexString = Right$(String$(7, "0") & Hex$(RGB), 8)
    R = CDbl("&H" & Mid$(HexString, 7, 2)) / 255
    G = CDbl("&H" & Mid$(HexString, 5, 2)) / 255
    B = CDbl("&H" & Mid$(HexString, 3, 2)) / 255

    RGB_Max = R
    If G > RGB_Max Then RGB_Max = G
    If B > RGB_Max Then RGB_Max = B

    RGB_Min = R
    If G < RGB_Min Then RGB_Min = G
    If B < RGB_Min Then RGB_Min = B

    RGB_Diff = RGB_Max - RGB_Min

    With RGBtoHSL
    
        .L = (RGB_Max + RGB_Min) / 2

        If RGB_Diff = 0 Then
    
            .S = 0
            .H = 0
    
        Else

            Select Case RGB_Max
                Case R: .H = (1 / 6) * (G - B) / RGB_Diff - (B > G)
                Case G: .H = (1 / 6) * (B - R) / RGB_Diff + (1 / 3)
                Case B: .H = (1 / 6) * (R - G) / RGB_Diff + (2 / 3)
            End Select
    
            Select Case .L
                Case Is < 0.5: .S = RGB_Diff / (2 * .L)
                Case Else:     .S = RGB_Diff / (2 - (2 * .L))
            End Select
    
        End If

    End With
    
End Function

Then, wherever you want a luminance value, just use the formula: [blue]=Luminance([green]address[/green])[/blue]. In your case put [blue]=Luminance(Sheet1!A1)[/blue] in cell A1 on sheet Sheet2, and copy to all the other cells in your grid.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Thats brilliant Tony thanks for your help, I don't have a clue how you've done it but it fixes the first hurdle!!

I will keep you guys posted on how my model progresses. You never know it may even advance to colour perception, scary...

Cheers

Sam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top