From 3f046bbba127e59ca53d48694de4575f70c72f37 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Sat, 15 Apr 2017 09:59:37 -0700 Subject: [PATCH] adding jet as a generator --- _examples/scatter/main.go | 2 +- drawing/color.go | 5 +++++ jet.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 jet.go diff --git a/_examples/scatter/main.go b/_examples/scatter/main.go index d270d57..06b89e0 100644 --- a/_examples/scatter/main.go +++ b/_examples/scatter/main.go @@ -17,7 +17,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { Show: true, StrokeWidth: chart.Disabled, DotWidth: 5, - DotColorProvider: chart.Viridis, + DotColorProvider: chart.Jet, }, XValues: chart.Sequence.Random(128, 1024), YValues: chart.Sequence.Random(128, 1024), diff --git a/drawing/color.go b/drawing/color.go index e7099b4..cfe0924 100644 --- a/drawing/color.go +++ b/drawing/color.go @@ -57,6 +57,11 @@ func ColorFromAlphaMixedRGBA(r, g, b, a uint32) Color { return c } +// ColorChannelFromFloat returns a normalized byte from a given float value. +func ColorChannelFromFloat(v float64) uint8 { + return uint8(v * 255) +} + // Color is our internal color type because color.Color is bullshit. type Color struct { R, G, B, A uint8 diff --git a/jet.go b/jet.go new file mode 100644 index 0000000..68d6f72 --- /dev/null +++ b/jet.go @@ -0,0 +1,37 @@ +package chart + +import "github.com/wcharczuk/go-chart/drawing" + +// Jet is a color map provider based on matlab's jet color map. +func Jet(xr, yr Range, x, y float64) drawing.Color { + return getJetColour(y, yr.GetMin(), yr.GetMax()) +} + +func getJetColour(v, vmin, vmax float64) drawing.Color { + c := drawing.Color{R: 0xff, G: 0xff, B: 0xff, A: 0xff} // white + var dv float64 + + if v < vmin { + v = vmin + } + if v > vmax { + v = vmax + } + dv = vmax - vmin + + if v < (vmin + 0.25*dv) { + c.R = 0 + c.G = drawing.ColorChannelFromFloat(4 * (v - vmin) / dv) + } else if v < (vmin + 0.5*dv) { + c.R = 0 + c.B = drawing.ColorChannelFromFloat(1 + 4*(vmin+0.25*dv-v)/dv) + } else if v < (vmin + 0.75*dv) { + c.R = drawing.ColorChannelFromFloat(4 * (v - vmin - 0.5*dv) / dv) + c.B = 0 + } else { + c.G = drawing.ColorChannelFromFloat(1 + 4*(vmin+0.75*dv-v)/dv) + c.B = 0 + } + + return c +}