Created
August 19, 2023 20:44
-
-
Save cbaragao/da41b9602ca8b57389766759d6d5117f to your computer and use it in GitHub Desktop.
Get the luminosity of a hex color with Power Query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Calculate luminosity based on hex value | |
| (HEX as text)=> | |
| let | |
| // Split strings into list by each character, convert to upper, remove hash | |
| Source = if HEX <> "" then Text.Upper(Text.AfterDelimiter(Text.From(HEX), "#")) else "#000000", | |
| // Split the Source by each character | |
| SplitHex = Splitter.SplitTextByRepeatedLengths(1)(Source), | |
| GetLuminosity = (hexlist)=> | |
| let | |
| // Build the RGB list | |
| RGB = List.Combine({{"0".."9"},{"A".."F"}}), | |
| // Sub function to figure out complimentary color RGB value | |
| GetRGB = (cval1, cval2)=> | |
| let | |
| RGBVal = (Number.From(cval1) * 16)+ Number.From(cval2) | |
| in | |
| RGBVal, | |
| // Get positions of each digit | |
| r = GetRGB(List.PositionOf(RGB,hexlist{0}), List.PositionOf(RGB, hexlist{1})), | |
| g = GetRGB(List.PositionOf(RGB, hexlist{2}), List.PositionOf(RGB, hexlist{3})), | |
| b = GetRGB(List.PositionOf(RGB,hexlist{4}), List.PositionOf(RGB,hexlist{5})), | |
| // Combine R,G,B hex values to one hex value | |
| FinalRGB = {r,g,b}, | |
| // Get the luminosity | |
| Luminosity = 0.5 * (List.Max(FinalRGB)/255 + List.Min(FinalRGB)/255) | |
| in | |
| Luminosity, | |
| // Process the split list from the input | |
| lum = GetLuminosity(SplitHex) | |
| in | |
| lum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment