Skip to content

Commit

Permalink
Implemented color parsing, experimenting with some color mixing for a…
Browse files Browse the repository at this point in the history
…ge groups, but I'm not too satisfied (#770)
  • Loading branch information
breki committed Dec 27, 2020
1 parent ee6c03f commit 0d4bf44
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/visualizations/Colors.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Colors

open System

type RgbaColor = uint32

let inline r(color: RgbaColor) = byte (color >>> 24)
Expand Down Expand Up @@ -35,3 +37,36 @@ let inline toHex(color: RgbaColor): string =
match alpha with
| 0xffuy -> sprintf "#%02x%02x%02x" (r color) (g color) (b color)
| _ -> sprintf "#%02x%02x%02x%02x" alpha (r color) (g color) (b color)

let fromHex (hexValue: string): RgbaColor =
let rec parseNextChar
(accumulatedValue: uint32) (hexChars: char list): uint32 =
match hexChars.Length with
| 0 -> accumulatedValue
| _ ->
let chr = Char.ToLower(hexChars.[0])
let digitValue: uint32 =
match chr with
| chr when chr >= '0' && chr <= '9' -> (uint32)chr - (uint32)'0'
| chr when chr >= 'a' && chr <= 'f' ->
(uint32)chr - (uint32)'a' + 10u
| _ -> invalidArg "hexValue" "Unsupported hex color value."

let accumulatedValueShifted =
(accumulatedValue <<< 4) ||| digitValue

parseNextChar accumulatedValueShifted (hexChars |> List.tail)

let parseToInt() =
match hexValue.[0] with
| '#' ->
let hexChars = hexValue |> Seq.toList |> List.tail
parseNextChar 0u hexChars
| _ -> invalidArg "hexValue" "Unsupported hex color value."

match hexValue.Length with
| 7 ->
let intValue = parseToInt()
(intValue <<< 8) ||| 0xffu

| _ -> invalidArg "hexValue" "Unsupported hex color value."
12 changes: 11 additions & 1 deletion src/visualizations/DataAnalysis/AgeGroupsTimeline.fs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,21 @@ let getAgeGroupTimelineAllSeriesData
if hasAnyNonZeroValues then
let points = ageGroupTimeline |> mapAllPoints

let baseColorOfAgeGroupHex = AgeGroup.colorOfAgeGroup index
let baseColorOfAgeGroup = Colors.fromHex baseColorOfAgeGroupHex
printfn "color=%A" baseColorOfAgeGroup
let mixinColor = Colors.fromHex "#8C71A8"
printfn "mixinColor=%A" mixinColor
let mixedColor =
Colors.mixColors baseColorOfAgeGroup mixinColor 0.3
let mixedColorHex = Colors.toHex mixedColor
printfn "mixedColorHex=%A" mixedColorHex

pojo {|
``type`` = "column"
visible = true
name = ageGroupKey.Label
color = AgeGroup.ColorOfAgeGroup index
color = mixedColorHex
data = points
animation = false
|} |> Some
Expand Down
2 changes: 1 addition & 1 deletion src/visualizations/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type AgeGroup =
Female : int option
All : int option } with

static member ColorOfAgeGroup ageGroupIndex =
static member colorOfAgeGroup ageGroupIndex =
let colors =
[| "#FFEEBA"; "#FFDA6B";"#E9B825";"#AEEFDB";"#52C4A2";"#33AB87"
"#189A73";"#F4B2E0";"#D559B0";"#B01C83" |]
Expand Down

0 comments on commit 0d4bf44

Please sign in to comment.