Skip to content

Instantly share code, notes, and snippets.

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

  • Save cbaragao/671743aaf2a173d55e6a99cad2e3b49f to your computer and use it in GitHub Desktop.

Select an option

Save cbaragao/671743aaf2a173d55e6a99cad2e3b49f to your computer and use it in GitHub Desktop.
Get the RGB value from a hex value
// Calculate RGB 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),
GetColor = (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})),
// Combine R,G,B hex values to one hex value
FinalRGB = "(" & Text.Combine({Text.From(r), Text.From(g), Text.From(b)}, ",") & ")"
in
FinalRGB,
// Process the split list from the input
RBGValues = GetColor(SplitHex)
in
RBGValues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment