Skip to content

Commit

Permalink
Implemented color functions from my Demeton project (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
breki committed Dec 26, 2020
1 parent 6b77d61 commit bc359e2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# TODO

- https://github.com/sledilnik/website/issues/770
- tooltips
- try using different colors
- using color mixing function
- tooltips

- new OWID export
- return back the official URL, once it has been pushed to production
Expand Down
1 change: 1 addition & 0 deletions src/visualizations/App.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Compile Include="IntersectionObserver.fs" />
<Compile Include="ShareButton.fs" />
<Compile Include="Highcharts.fs" />
<Compile Include="Colors.fs" />
<Compile Include="Utils.fs" />
<Compile Include="Days.fs" />
<Compile Include="Statistics.fs" />
Expand Down
37 changes: 37 additions & 0 deletions src/visualizations/Colors.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Colors

type RgbaColor = uint32

let inline r(color: RgbaColor) = byte (color >>> 24)
let inline g(color: RgbaColor) = byte (color >>> 16)
let inline b(color: RgbaColor) = byte (color >>> 8)
let inline a(color: RgbaColor) = byte color

/// <summary>
/// Constructs the <see cref="RgbaColor" /> from R, G, B and alpha
/// components.
/// </summary>
let inline rgbaColor (r: byte) (g: byte) (b: byte) (a: byte): RgbaColor =
(uint32 r <<< 24) ||| (uint32 g <<< 16) ||| (uint32 b <<< 8) ||| (uint32 a)

let inline mixColors colorA colorB mixRatio =
let mixByteValues (v1:byte) (v2: byte): byte =
let v1Float = float v1
let mixedFloat = (float v2 - v1Float) * mixRatio + v1Float
byte mixedFloat

match mixRatio with
| 0. -> colorA
| 1. -> colorB
| _ ->
rgbaColor
(mixByteValues (r colorA) (r colorB))
(mixByteValues (g colorA) (g colorB))
(mixByteValues (b colorA) (b colorB))
(mixByteValues (a colorA) (a colorB))

let inline toHex(color: RgbaColor): string =
let alpha = a color
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)

0 comments on commit bc359e2

Please sign in to comment.