Skip to content

Instantly share code, notes, and snippets.

@tonmcg
Created May 29, 2018 13:52
Show Gist options
  • Save tonmcg/544eaace9b615b73478f02da03b73e20 to your computer and use it in GitHub Desktop.
Save tonmcg/544eaace9b615b73478f02da03b73e20 to your computer and use it in GitHub Desktop.
M Language Custom Report Themes Functions
let
Theme.ColorAPI = (hexColorCode as text, optional numberOfColors as number) =>
let
number = if numberOfColors is null then 11 else numberOfColors,
Source = Json.Document(Web.Contents("http://www.thecolorapi.com/scheme?hex=" & hexColorCode & "&mode=analogic-complement&count=" & Number.ToText(number))),
colors = Source[colors],
ConvertedToTable = Table.FromList(colors, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
ExpandedColors = Table.ExpandRecordColumn(ConvertedToTable, "Column1", {"hex", "rgb", "hsl", "hsv", "name", "cmyk", "XYZ", "image", "contrast", "_links", "_embedded"}, {"hex", "rgb", "hsl", "hsv", "name", "cmyk", "XYZ", "image", "contrast", "_links", "_embedded"}),
ExpandedHex = Table.ExpandRecordColumn(ExpandedColors, "hex", {"value"}, {"value"}),
colorName =
let
name = ExpandedHex[name]{0}[value]
in
name,
CreatedTable = #table({"name", "dataColors", "background", "foreground", "tableAccent"},{{colorName, ExpandedHex[value], "#ffffff", ExpandedHex[value]{1}, ExpandedHex[value]{0}}}),
ConvertedToJSON = Text.FromBinary(Json.FromValue(CreatedTable)),
customReportFormat = Text.TrimStart(Text.TrimEnd(ConvertedToJSON, {"]"}), {"["})
in
customReportFormat,
DefineDocs = [
Documentation.Name = " Theme.ColorAPI",
Documentation.Description = " Returns a JSON string, formatted for use as a Custom Report Theme in Power BI.",
Documentation.LongDescription = " Returns a properly-formatted JSON string containing the hexadecimal representation of colors that comprise palettes on the http://www.thecolorapi.com/ website. The hexColorCode parameter defines the base color of the returned palette. The optional numberOfColors parameter defines the number of colors (up to 11) the resulting palette contains. The JSON string can be used as a Custom Report Theme in Power BI.",
Documentation.Category = " Text.Manipulation",
Documentation.Source = " ",
Documentation.Author = " Tony McGovern, emdata.ai",
Documentation.Examples = {
[
Description = " Return the a color palette as a JSON-formatted string to use as a Report Theme in Power BI.",
Code = " ColorAPI(""326ada"",null)",
Result = "{""name"":""Luxor Gold"",""dataColors"":[""#A07A2C"",""#AC812C"",""#B8892C"",""#C4912B"",""#8230D9"",""#A531DA"",""#C933DA"",""#DB34CB"",""#DC36AA"",""#DD3789"",""#DE3868""],""background"":""#ffffff"",""foreground"":""#AC812C"",""tableAccent"":""#A07A2C""}"
]
}
]
in
Value.ReplaceType(
Theme.ColorAPI,
Value.ReplaceMetadata(
Value.Type(Theme.ColorAPI),
DefineDocs
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment