4 people like it.

Heatmap Color Generating Function

Function to generate color values to reflect the relative intensity of numeric values in a range. --- Fixed edge case of when max=min

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
#if INTERACTIVE
#r "WindowsBase"
#r "PresentationCore"
#else
module Heatmap
#endif
open System.Windows.Media
open System

let colorIntensity (v:float) min max (colorRange:Color seq) =
    if v < min || v > max then failwithf "value %0.3f should be between min (%0.3f) and max (%0.3f)" v min max
    let nV = if max=min then 1.f else (v - min) / (max - min) |> float32 // between 0..1
    let steps = colorRange |> Seq.length
    let step = 1.f / float32 steps
    let minV = nV - step
    let maxV = nV + step
    let (<->) x (a,b) = x>=a && x<=b //between operator
    seq {for i in 0..steps -> (float32 i) * step} 
    |> Seq.windowed 2 
    |> Seq.map(fun r -> r.[0],r.[1])
    |> Seq.map(fun (a,b) ->
        if nV <-> (a,b) then abs((a-b)/2.0f-nV)
        elif minV <-> (a,b) then step-(nV-b)
        elif maxV <-> (a,b) then step-(a-nV)
        else 0.0f)
    |> Seq.zip colorRange 
    |> Seq.map (fun (c,s) -> s,Color.FromScRgb(1.f, c.ScR*s, c.ScG*s, c.ScB*s))
    |> Seq.filter (fun (a,c) -> a>0.f)
    |> Seq.map (fun (_,c)-> c.Clamp(); c)
    |> Seq.fold (+) (Color.FromScRgb(1.f,0.f,0.f,0.f))
(*
colorIntensity 0.1 0. 1. [Colors.Blue;Colors.LightGreen;Colors.Yellow;Colors.Red]
*)
namespace System
namespace System.Windows
namespace System.Windows.Media
val colorIntensity : v:float -> min:float -> max:float -> colorRange:seq<Color> -> Color

Full name: Script.colorIntensity
val v : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val min : float
val max : float
val colorRange : seq<Color>
type Color =
  struct
    member A : byte with get, set
    member B : byte with get, set
    member Clamp : unit -> unit
    member ColorContext : ColorContext
    member Equals : color:Color -> bool + 1 overload
    member G : byte with get, set
    member GetHashCode : unit -> int
    member GetNativeColorValues : unit -> float32[]
    member R : byte with get, set
    member ScA : float32 with get, set
    ...
  end

Full name: System.Windows.Media.Color
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val failwithf : format:Printf.StringFormat<'T,'Result> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.failwithf
val nV : float32
Multiple items
val float32 : value:'T -> float32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float32

--------------------
type float32 = Single

Full name: Microsoft.FSharp.Core.float32

--------------------
type float32<'Measure> = float32

Full name: Microsoft.FSharp.Core.float32<_>
val steps : int
module Seq

from Microsoft.FSharp.Collections
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
val step : float32
val minV : float32
val maxV : float32
val x : 'a (requires comparison)
val a : 'a (requires comparison)
val b : 'a (requires comparison)
val i : int
val windowed : windowSize:int -> source:seq<'T> -> seq<'T []>

Full name: Microsoft.FSharp.Collections.Seq.windowed
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val r : float32 []
val a : float32
val b : float32
val abs : value:'T -> 'T (requires member Abs)

Full name: Microsoft.FSharp.Core.Operators.abs
val zip : source1:seq<'T1> -> source2:seq<'T2> -> seq<'T1 * 'T2>

Full name: Microsoft.FSharp.Collections.Seq.zip
val c : Color
val s : float32
Color.FromScRgb(a: float32, r: float32, g: float32, b: float32) : Color
property Color.ScR: float32
property Color.ScG: float32
property Color.ScB: float32
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
Color.Clamp() : unit
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold

More information

Link:http://fssnip.net/cN
Posted:11 years ago
Author:Faisal Waris
Tags: heatmap; wpf