-
Notifications
You must be signed in to change notification settings - Fork 0
/
colormap.go
304 lines (270 loc) · 10.2 KB
/
colormap.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2019, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package giv
import (
"math"
"reflect"
"sort"
"github.com/chewxy/math32"
"github.com/goki/gi/gi"
"github.com/goki/gi/gist"
"github.com/goki/gi/oswin"
"github.com/goki/gi/oswin/mouse"
"github.com/goki/gi/units"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
"github.com/goki/mat32"
)
// ColorMap maps a value onto a color by interpolating between a list of colors
// defining a spectrum
type ColorMap struct {
Name string
NoColor gist.Color `desc:"color to display for invalid numbers (e.g., NaN)"`
Colors []gist.Color `desc:"list of colors to interpolate between"`
}
// Map returns color for normalized value in range 0-1. NaN returns NoColor
// which can be used to indicate missing values.
func (cm *ColorMap) Map(val float64) gist.Color {
nc := len(cm.Colors)
if nc < 2 {
return gist.Color{}
}
if math.IsNaN(val) {
return cm.NoColor
}
if val <= 0 {
return cm.Colors[0]
} else if val >= 1 {
return cm.Colors[nc-1]
}
ival := val * float64(nc-1)
lidx := math.Floor(ival)
uidx := math.Ceil(ival)
if lidx == uidx {
return cm.Colors[int(lidx)]
}
cmix := ival - lidx
lclr := cm.Colors[int(lidx)]
uclr := cm.Colors[int(uidx)]
return lclr.Blend(float32(cmix)*100, uclr)
}
// see https://matplotlib.org/tutorials/colors/colormap-manipulation.html
// for how to read out matplotlib scales -- still don't understand segmented ones!
// StdColorMaps is a list of standard color maps
var StdColorMaps = map[string]*ColorMap{
"ColdHot": {"ColdHot", gist.Color{200, 200, 200, 255}, []gist.Color{{0, 255, 255, 255}, {0, 0, 255, 255}, {127, 127, 127, 255}, {255, 0, 0, 255}, {255, 255, 0, 255}}},
"Jet": {"Jet", gist.Color{200, 200, 200, 255}, []gist.Color{{0, 0, 127, 255}, {0, 0, 255, 255}, {0, 127, 255, 255}, {0, 255, 255, 255}, {127, 255, 127, 255}, {255, 255, 0, 255}, {255, 127, 0, 255}, {255, 0, 0, 255}, {127, 0, 0, 255}}},
"JetMuted": {"JetMuted", gist.Color{200, 200, 200, 255}, []gist.Color{{25, 25, 153, 255}, {25, 102, 230, 255}, {0, 230, 230, 255}, {0, 179, 0, 255}, {230, 230, 0, 255}, {230, 102, 25, 255}, {153, 25, 25, 255}}},
"Viridis": {"Viridis", gist.Color{200, 200, 200, 255}, []gist.Color{{72, 33, 114, 255}, {67, 62, 133, 255}, {56, 87, 140, 255}, {45, 111, 142, 255}, {36, 133, 142, 255}, {30, 155, 138, 255}, {42, 176, 127, 255}, {81, 197, 105, 255}, {134, 212, 73, 255}, {194, 223, 35, 255}, {253, 231, 37, 255}}},
"Plasma": {"Plasma", gist.Color{200, 200, 200, 255}, []gist.Color{{61, 4, 155, 255}, {99, 0, 167, 255}, {133, 6, 166, 255}, {166, 32, 152, 255}, {192, 58, 131, 255}, {213, 84, 110, 255}, {231, 111, 90, 255}, {246, 141, 69, 255}, {253, 174, 50, 255}, {252, 210, 36, 255}, {240, 248, 33, 255}}},
"Inferno": {"Inferno", gist.Color{200, 200, 200, 255}, []gist.Color{{37, 12, 3, 255}, {19, 11, 52, 255}, {57, 9, 99, 255}, {95, 19, 110, 255}, {133, 33, 107, 255}, {169, 46, 94, 255}, {203, 65, 73, 255}, {230, 93, 47, 255}, {247, 131, 17, 255}, {252, 174, 19, 255}, {245, 219, 76, 255}, {252, 254, 164, 255}}},
"BlueBlackRed": {"BlueBlackRed", gist.Color{200, 200, 200, 255}, []gist.Color{{0, 0, 255, 255}, {76, 76, 76, 255}, {255, 0, 0, 255}}},
"BlueGreyRed": {"BlueGreyRed", gist.Color{200, 200, 200, 255}, []gist.Color{{0, 0, 255, 255}, {127, 127, 127, 255}, {255, 0, 0, 255}}},
"BlueWhiteRed": {"BlueWhiteRed", gist.Color{200, 200, 200, 255}, []gist.Color{{0, 0, 255, 255}, {230, 230, 230, 255}, {255, 0, 0, 255}}},
"BlueGreenRed": {"BlueGreenRed", gist.Color{200, 200, 200, 255}, []gist.Color{{0, 0, 255, 255}, {0, 230, 0, 255}, {255, 0, 0, 255}}},
"Rainbow": {"Rainbow", gist.Color{200, 200, 200, 255}, []gist.Color{{255, 0, 255, 255}, {0, 0, 255, 255}, {0, 255, 0, 255}, {255, 255, 0, 255}, {255, 0, 0, 255}}},
"ROYGBIV": {"ROYGBIV", gist.Color{200, 200, 200, 255}, []gist.Color{{255, 0, 255, 255}, {0, 0, 127, 255}, {0, 0, 255, 255}, {0, 255, 0, 255}, {255, 255, 0, 255}, {255, 0, 0, 255}}},
"DarkLight": {"DarkLight", gist.Color{200, 200, 200, 255}, []gist.Color{{0, 0, 0, 255}, {250, 250, 250, 255}}},
"DarkLightDark": {"DarkLightDark", gist.Color{200, 200, 200, 255}, []gist.Color{{0, 0, 0, 255}, {250, 250, 250, 255}, {0, 0, 0, 255}}},
"LightDarkLight": {"DarkLightDark", gist.Color{200, 200, 200, 255}, []gist.Color{{250, 250, 250, 255}, {0, 0, 0, 255}, {250, 250, 250, 255}}},
}
// AvailColorMaps is the list of all available color maps
var AvailColorMaps = map[string]*ColorMap{}
func init() {
for k, v := range StdColorMaps {
AvailColorMaps[k] = v
}
}
// AvailColorMapsList returns a sorted list of color map names, e.g., for choosers
func AvailColorMapsList() []string {
sl := make([]string, len(AvailColorMaps))
ctr := 0
for k := range AvailColorMaps {
sl[ctr] = k
ctr++
}
sort.Strings(sl)
return sl
}
// ColorMapName provides a gui chooser of maps in AvailColorMaps
type ColorMapName string
/////////////////////////////////////////////////////////////////////////////
// ColorMapView
// ColorMapView is a widget that displays a ColorMap.
// Note that this is not a ValueView widget
type ColorMapView struct {
gi.WidgetBase
Orient mat32.Dims `desc:"orientation along which to display the spectrum"`
Map *ColorMap `desc:"the colormap that we view"`
ColorMapSig ki.Signal `json:"-" xml:"-" view:"-" desc:"signal for color map -- triggers when new color map is set via chooser"`
}
var KiT_ColorMapView = kit.Types.AddType(&ColorMapView{}, nil)
// AddNewColorMapView adds a new colorview to given parent node, with given name.
func AddNewColorMapView(parent ki.Ki, name string, cmap *ColorMap) *ColorMapView {
cv := parent.AddNewChild(KiT_ColorMapView, name).(*ColorMapView)
cv.Map = cmap
return cv
}
func (cv *ColorMapView) Disconnect() {
cv.WidgetBase.Disconnect()
cv.ColorMapSig.DisconnectAll()
}
// SetColorMap sets the color map and triggers a display update
func (cv *ColorMapView) SetColorMap(cmap *ColorMap) {
cv.Map = cmap
cv.UpdateSig()
}
// SetColorMapAction sets the color map and triggers a display update
// and signals the ColorMapSig signal
func (cv *ColorMapView) SetColorMapAction(cmap *ColorMap) {
cv.Map = cmap
cv.ColorMapSig.Emit(cv.This(), 0, nil)
cv.UpdateSig()
}
// ChooseColorMap pulls up a chooser to select a color map
func (cv *ColorMapView) ChooseColorMap() {
sl := AvailColorMapsList()
cur := ""
if cv.Map != nil {
cur = cv.Map.Name
}
SliceViewSelectDialog(cv.Viewport, &sl, cur, DlgOpts{Title: "Select a ColorMap", Prompt: "choose color map to use from among available list"}, nil,
cv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
ddlg := send.Embed(gi.KiT_Dialog).(*gi.Dialog)
si := SliceViewSelectDialogValue(ddlg)
if si >= 0 {
nmap, ok := AvailColorMaps[sl[si]]
if ok {
cv.SetColorMapAction(nmap)
}
}
}
})
}
// MouseEvent handles button MouseEvent
func (cv *ColorMapView) MouseEvent() {
cv.ConnectEvent(oswin.MouseEvent, gi.RegPri, func(recv, send ki.Ki, sig int64, d interface{}) {
me := d.(*mouse.Event)
cvv := recv.(*ColorMapView)
if me.Button == mouse.Left {
switch me.Action {
case mouse.DoubleClick: // we just count as a regular click
fallthrough
case mouse.Press:
me.SetProcessed()
cvv.ChooseColorMap()
}
}
})
}
func (cv *ColorMapView) ConnectEvents2D() {
cv.MouseEvent()
cv.HoverTooltipEvent()
}
func (cv *ColorMapView) RenderColorMap() {
if cv.Map == nil {
cv.Map = StdColorMaps["ColdHot"]
}
rs := cv.Render()
rs.Lock()
pc := &rs.Paint
pos := cv.LayState.Alloc.Pos
sz := cv.LayState.Alloc.Size
lsz := sz.Dim(cv.Orient)
inc := math32.Ceil(lsz / 100)
if inc < 2 {
inc = 2
}
sr := sz
pr := pos
sp := pr.Dim(cv.Orient)
sr.SetDim(cv.Orient, inc)
for p := float32(0); p < lsz; p += inc {
val := p / (lsz - 1)
clr := cv.Map.Map(float64(val))
pr.SetDim(cv.Orient, sp+p)
pc.FillBoxColor(rs, pr, sr, clr)
}
rs.Unlock()
}
func (cv *ColorMapView) Render2D() {
if cv.FullReRenderIfNeeded() {
return
}
if cv.PushBounds() {
cv.This().(gi.Node2D).ConnectEvents2D()
cv.RenderColorMap()
cv.Render2DChildren()
cv.PopBounds()
} else {
cv.DisconnectAllEvents(gi.RegPri)
}
}
////////////////////////////////////////////////////////////////////////////////////////
// ColorMapValueView
// ValueView registers ColorMapValueView as the viewer of ColorMapName
func (mn ColorMapName) ValueView() ValueView {
vv := ColorMapValueView{}
vv.Init(&vv)
return &vv
}
// ColorMapValueView presents an action for displaying a ColorMapName and selecting
// meshes from a ChooserDialog
type ColorMapValueView struct {
ValueViewBase
}
var KiT_ColorMapValueView = kit.Types.AddType(&ColorMapValueView{}, nil)
func (vv *ColorMapValueView) WidgetType() reflect.Type {
vv.WidgetTyp = gi.KiT_Action
return vv.WidgetTyp
}
func (vv *ColorMapValueView) UpdateWidget() {
if vv.Widget == nil {
return
}
ac := vv.Widget.(*gi.Action)
txt := kit.ToString(vv.Value.Interface())
if txt == "" {
txt = "(none, click to select)"
}
ac.SetText(txt)
}
func (vv *ColorMapValueView) ConfigWidget(widg gi.Node2D) {
vv.Widget = widg
vv.StdConfigWidget(widg)
ac := vv.Widget.(*gi.Action)
ac.SetProp("border-radius", units.NewPx(4))
ac.ActionSig.ConnectOnly(vv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
vvv, _ := recv.Embed(KiT_ColorMapValueView).(*ColorMapValueView)
ac := vvv.Widget.(*gi.Action)
vvv.Activate(ac.Viewport, nil, nil)
})
vv.UpdateWidget()
}
func (vv *ColorMapValueView) HasAction() bool {
return true
}
func (vv *ColorMapValueView) Activate(vp *gi.Viewport2D, dlgRecv ki.Ki, dlgFunc ki.RecvFunc) {
if vv.IsInactive() {
return
}
sl := AvailColorMapsList()
cur := kit.ToString(vv.Value.Interface())
desc, _ := vv.Tag("desc")
SliceViewSelectDialog(vp, &sl, cur, DlgOpts{Title: "Select a ColorMap", Prompt: desc}, nil,
vv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
ddlg := send.Embed(gi.KiT_Dialog).(*gi.Dialog)
si := SliceViewSelectDialogValue(ddlg)
if si >= 0 {
vv.SetValue(sl[si])
vv.UpdateWidget()
}
}
if dlgRecv != nil && dlgFunc != nil {
dlgFunc(dlgRecv, send, sig, data)
}
})
}