Skip to content

Instantly share code, notes, and snippets.

@mlongoria
Created August 16, 2020 00:27
Show Gist options
  • Save mlongoria/bece39898836e8374c89a774df66fc68 to your computer and use it in GitHub Desktop.
Save mlongoria/bece39898836e8374c89a774df66fc68 to your computer and use it in GitHub Desktop.
Color Contrast Math
let
//Get values 0 - 255
Source = Values,
//Call that column R for Red
#"R Dec" = Table.RenameColumns(Source,{{"Column1", "R Dec"}}),
//Crossjoin to Values to get Green values 0 - 255
#"G Dec" = Table.AddColumn(#"R Dec", "Custom", each Values),
#"Expanded G Dec" = Table.ExpandTableColumn(#"G Dec", "Custom", {"Column1"}, {"G Dec"}),
//Crossjoin to Values to get Blue values 0 - 255
#"B Dec" = Table.AddColumn(#"Expanded G Dec", "B", each Values),
#"Expanded B Dec" = Table.ExpandTableColumn(#"B Dec", "B", {"Column1"}, {"B Dec"}),
//Get hexidecimal values for R,G,B
#"R Hex" = Table.AddColumn(#"Expanded B Dec", "R Hex", each Text.End("000" & Number.ToText([R Dec], "x"),2)),
#"G Hex" = Table.AddColumn(#"R Hex", "G Hex", each Text.End("000" & Number.ToText([G Dec], "x"),2)),
#"B Hex" = Table.AddColumn(#"G Hex", "B Hex", each Text.End("000" & Number.ToText([B Dec], "x"),2)),
//Concatenate to get full 6-digit Hex color value
#"Changed Hex Type" = Table.TransformColumnTypes(#"B Hex",{{"R Hex", type text}, {"G Hex", type text}, {"B Hex", type text}}),
#"Full Hex" = Table.AddColumn(#"Changed Hex Type", "Hex", each [R Hex] & [G Hex] & [B Hex]),
//Convert integers to decimals and linearize
#"R Lin" = Table.AddColumn(#"Full Hex", "R Lin", each ColorConvert(([R Dec]/255))),
#"G Lin" = Table.AddColumn(#"R Lin", "G Lin", each ColorConvert(([G Dec]/255))),
#"B Lin" = Table.AddColumn(#"G Lin", "B Lin", each ColorConvert(([B Dec]/255))),
//Calculate luminance with the linearized values
#"Luminance" = Table.AddColumn(#"B Lin", "Luminance", each 0.2126 * [R Lin] + 0.7152 * [G Lin] + 0.0722 * [B Lin]),
#"Changed Luminance Type" = Table.TransformColumnTypes(#"Luminance",{{"Luminance", type number}}),
//Create a column for hexidecimal value with the hash/pound at the beginning
#"Hex Dup" = Table.DuplicateColumn(#"Changed Luminance Type", "Hex", "Hex With Hash"),
#"Hex with Hash" = Table.TransformColumns(#"Hex Dup", {{"Hex With Hash", each "#" & _, type text}}),
//Remove Hex and linearized RGB columns to keep model under 1 GB limit for Pro license
#"Removed Columns" = Table.RemoveColumns(#"Hex with Hash",{"R Hex", "G Hex", "B Hex", "R Lin", "G Lin", "B Lin", "Hex"}),
//Rename Hex with Hash to Hex
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Hex With Hash", "Hex"}})
in
#"Renamed Columns"
let
Source = #"Color 1"
in
Source
(colornum as number) =>
let
Source = if colornum < .04045 then colornum/12.92 else Number.Power(((colornum+0.055)/1.055),2.4)
in
Source
let
Source = List.Numbers(0,256),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", Int64.Type}})
in
#"Changed Type"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment