Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Created August 19, 2023 22:15
Show Gist options
  • Select an option

  • Save cbaragao/0ff61dc54f639b8509e6e236b150b9a0 to your computer and use it in GitHub Desktop.

Select an option

Save cbaragao/0ff61dc54f639b8509e6e236b150b9a0 to your computer and use it in GitHub Desktop.
Get the hue of a hex value using Power Query
(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),
GetHue = (hexlist)=>
let
// Build the RGB list
RGB = List.Combine({{"0".."9"},{"A".."F"}}),
// Sub function to figure out 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})),
// Get the hue from r,g,b values
hue =
if r >= g and g >= b then 60 * (g-b)/(r-b)
else if g > r and r>= b then 60 * (2-(r-b)/(g-b))
else if g >= b and b > r then 60 * (2+(b-r)/(g-r))
else if b > g and g > r then 60 * (4- (g-r)/(b-r))
else if b > r and r >= g then 60 * (4+(r-g)/(b-g))
else if r >= b and b > g then 60 * (6-(b-g)/(r-g))
else -1
in
hue,
// Process the split list from the input
ColorHue = GetHue(SplitHex)
in
ColorHue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment