1 people like it.

Heatmap Color generation for RGB Colors

Works with System.Drawing Color

 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: 
open System.Drawing

let scaler (sMin,sMax) (vMin,vMax) (v:float) =
    if v < vMin then failwith "out of min range for scaling"
    if v > vMax then failwith "out of max range for scaling"
    (v - vMin) / (vMax - vMin) * (sMax - sMin) + sMin

let createColor (r,g,b) = Color.FromArgb(255, min r 255, min g 255, min b 255)

let interpolate (c1:Color) (c2:Color) (m:float) =
    createColor(
        (1. - m) * float c1.R + float c2.R * m |> int,
        (1. - m) * float c1.G + float c2.G * m |> int,
        (1. - m) * float c1.B + float c2.B * m |> int
    )

let cI (v:float) mn mx (colorRange:Color[]) =
    if v < mn || v > mx then failwithf "value %0.3f should be between min (%0.3f) and max (%0.3f)" v mn mx
    let cindex = scaler (0., float (colorRange.Length - 1)) (mn,mx) v
    let c1 = colorRange.[floor cindex |> int]
    let c2 = colorRange.[ceil cindex |> int]
    let prop = cindex - (floor cindex)
    interpolate c1 c2 prop

//usage:
// cI 0.3 0. 1. [|Color.Blue; Color.Yellow; Color.Red|]
namespace System
namespace System.Drawing
val scaler : sMin:float * sMax:float -> vMin:float * vMax:float -> v:float -> float

Full name: Script.scaler
val sMin : float
val sMax : float
val vMin : float
val vMax : float
val v : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

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

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

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

Full name: Microsoft.FSharp.Core.float<_>
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val createColor : r:int * g:int * b:int -> Color

Full name: Script.createColor
val r : int
val g : int
val b : int
type Color =
  struct
    member A : byte
    member B : byte
    member Equals : obj:obj -> bool
    member G : byte
    member GetBrightness : unit -> float32
    member GetHashCode : unit -> int
    member GetHue : unit -> float32
    member GetSaturation : unit -> float32
    member IsEmpty : bool
    member IsKnownColor : bool
    ...
  end

Full name: System.Drawing.Color
Color.FromArgb(argb: int) : Color
Color.FromArgb(alpha: int, baseColor: Color) : Color
Color.FromArgb(red: int, green: int, blue: int) : Color
Color.FromArgb(alpha: int, red: int, green: int, blue: int) : Color
val min : e1:'T -> e2:'T -> 'T (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.min
val interpolate : c1:Color -> c2:Color -> m:float -> Color

Full name: Script.interpolate
val c1 : Color
val c2 : Color
val m : float
property Color.R: byte
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
property Color.G: byte
property Color.B: byte
val cI : v:float -> mn:float -> mx:float -> colorRange:Color [] -> Color

Full name: Script.cI
val mn : float
val mx : float
val colorRange : Color []
val failwithf : format:Printf.StringFormat<'T,'Result> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.failwithf
val cindex : float
property System.Array.Length: int
val floor : value:'T -> 'T (requires member Floor)

Full name: Microsoft.FSharp.Core.Operators.floor
val ceil : value:'T -> 'T (requires member Ceiling)

Full name: Microsoft.FSharp.Core.Operators.ceil
val prop : float
Raw view Test code New version

More information

Link:http://fssnip.net/7Ri
Posted:7 years ago
Author:Faisal Waris
Tags: drawing , windows forms