File: heatmap_printer.go

package info (click to toggle)
golang-github-pterm-pterm 0.12.79-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,640 kB
  • sloc: makefile: 4
file content (744 lines) | stat: -rw-r--r-- 20,217 bytes parent folder | download
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
package pterm

import (
	"bytes"
	"errors"
	"io"
	"math"
	"strings"

	"github.com/pterm/pterm/internal"
)

// DefaultHeatmap contains standards, which can be used to print a HeatmapPrinter.
var DefaultHeatmap = HeatmapPrinter{
	AxisStyle:                  &ThemeDefault.HeatmapHeaderStyle,
	SeparatorStyle:             &ThemeDefault.HeatmapSeparatorStyle,
	VerticalSeparator:          "│",
	TopRightCornerSeparator:    "└",
	TopLeftCornerSeparator:     "┘",
	BottomLeftCornerSeparator:  "┐",
	BottomRightCornerSeparator: "┌",
	HorizontalSeparator:        "─",
	TSeparator:                 "┬",
	TReverseSeparator:          "┴",
	LSeparator:                 "├",
	LReverseSeparator:          "┤",
	TCrossSeparator:            "┼",
	LegendLabel:                "Legend",
	Boxed:                      true,
	Grid:                       true,
	Legend:                     true,
	TextRGB:                    RGB{0, 0, 0, false},
	RGBRange:                   []RGB{{R: 255, G: 0, B: 0, Background: true}, {R: 255, G: 165, B: 0, Background: true}, {R: 0, G: 255, B: 0, Background: true}},
	TextColor:                  FgBlack,
	Colors:                     []Color{BgRed, BgLightRed, BgYellow, BgLightYellow, BgLightGreen, BgGreen},

	EnableRGB: false,
}

// HeatmapData is the type that contains the data of a HeatmapPrinter.
type HeatmapData [][]float32

type HeatmapAxis struct {
	XAxis []string
	YAxis []string
}

// HeatmapPrinter is able to render tables.
type HeatmapPrinter struct {
	HasHeader                  bool
	AxisStyle                  *Style
	VerticalSeparator          string
	TopRightCornerSeparator    string
	TopLeftCornerSeparator     string
	BottomLeftCornerSeparator  string
	BottomRightCornerSeparator string
	HorizontalSeparator        string
	TSeparator                 string
	TReverseSeparator          string
	LSeparator                 string
	LReverseSeparator          string
	TCrossSeparator            string
	LegendLabel                string
	SeparatorStyle             *Style
	Data                       HeatmapData
	Axis                       HeatmapAxis
	Boxed                      bool
	Grid                       bool
	OnlyColoredCells           bool
	LegendOnlyColoredCells     bool
	EnableComplementaryColor   bool
	Legend                     bool
	CellSize                   int
	Colors                     []Color
	TextColor                  Color
	EnableRGB                  bool
	RGBRange                   []RGB
	TextRGB                    RGB
	Writer                     io.Writer

	minValue float32
	maxValue float32

	rgbLegendValue int
}

var complementaryColors = map[Color]Color{
	BgBlack:        FgLightWhite,
	BgRed:          FgCyan,
	BgGreen:        FgMagenta,
	BgYellow:       FgBlue,
	BgBlue:         FgYellow,
	BgMagenta:      FgGreen,
	BgCyan:         FgRed,
	BgWhite:        FgBlack,
	BgDefault:      FgBlack,
	BgDarkGray:     FgLightWhite,
	BgLightRed:     FgLightCyan,
	BgLightGreen:   FgLightMagenta,
	BgLightYellow:  FgLightBlue,
	BgLightBlue:    FgLightYellow,
	BgLightMagenta: FgLightGreen,
	BgLightCyan:    FgLightRed,
	BgLightWhite:   FgBlack,
}

// WithAxisData returns a new HeatmapPrinter, where the first line and row are headers.
func (p HeatmapPrinter) WithAxisData(hd HeatmapAxis) *HeatmapPrinter {
	p.HasHeader = true
	p.Axis = hd
	return &p
}

// WithAxisStyle returns a new HeatmapPrinter with a specific AxisStyle.
func (p HeatmapPrinter) WithAxisStyle(style *Style) *HeatmapPrinter {
	p.AxisStyle = style
	return &p
}

// WithSeparatorStyle returns a new HeatmapPrinter with a specific SeparatorStyle.
func (p HeatmapPrinter) WithSeparatorStyle(style *Style) *HeatmapPrinter {
	p.SeparatorStyle = style
	return &p
}

// WithData returns a new HeatmapPrinter with specific Data.
func (p HeatmapPrinter) WithData(data [][]float32) *HeatmapPrinter {
	p.Data = data
	return &p
}

// WithTextColor returns a new HeatmapPrinter with a specific TextColor.
// This sets EnableComplementaryColor to false.
func (p HeatmapPrinter) WithTextColor(color Color) *HeatmapPrinter {
	p.TextColor = color
	p.EnableComplementaryColor = false
	return &p
}

// WithTextRGB returns a new HeatmapPrinter with a specific TextRGB.
// This sets EnableComplementaryColor to false.
func (p HeatmapPrinter) WithTextRGB(rgb RGB) *HeatmapPrinter {
	p.TextRGB = rgb
	p.EnableComplementaryColor = false
	return &p
}

// WithBoxed returns a new HeatmapPrinter with a box around the table.
// If set to true, Grid will be set to true too.
func (p HeatmapPrinter) WithBoxed(b ...bool) *HeatmapPrinter {
	p.Boxed = internal.WithBoolean(b)
	if p.Boxed && !p.Grid {
		p.Grid = true
	}
	return &p
}

// WithGrid returns a new HeatmapPrinter with a grid.
// If set to false, Boxed will be set to false too.
func (p HeatmapPrinter) WithGrid(b ...bool) *HeatmapPrinter {
	b2 := internal.WithBoolean(b)
	p.Grid = b2
	if !b2 && p.Boxed {
		p.Boxed = false
	}
	return &p
}

// WithEnableRGB returns a new HeatmapPrinter with RGB colors.
func (p HeatmapPrinter) WithEnableRGB(b ...bool) *HeatmapPrinter {
	p.EnableRGB = internal.WithBoolean(b)
	return &p
}

// WithOnlyColoredCells returns a new HeatmapPrinter with only colored cells.
func (p HeatmapPrinter) WithOnlyColoredCells(b ...bool) *HeatmapPrinter {
	b2 := internal.WithBoolean(b)
	p.OnlyColoredCells = b2
	return &p
}

// WithLegendOnlyColoredCells returns a new HeatmapPrinter with legend with only colored cells.
// This sets the Legend to true.
func (p HeatmapPrinter) WithLegendOnlyColoredCells(b ...bool) *HeatmapPrinter {
	b2 := internal.WithBoolean(b)
	p.LegendOnlyColoredCells = b2
	if b2 {
		p.Legend = true
	}
	return &p
}

// WithEnableComplementaryColor returns a new HeatmapPrinter with complement color.
func (p HeatmapPrinter) WithEnableComplementaryColor(b ...bool) *HeatmapPrinter {
	p.EnableComplementaryColor = internal.WithBoolean(b)
	return &p
}

// WithLegend returns a new HeatmapPrinter with a legend.
func (p HeatmapPrinter) WithLegend(b ...bool) *HeatmapPrinter {
	p.Legend = internal.WithBoolean(b)
	return &p
}

// WithCellSize returns a new HeatmapPrinter with a specific cell size.
// This only works if there is no header and OnlyColoredCells == true!
func (p HeatmapPrinter) WithCellSize(i int) *HeatmapPrinter {
	p.CellSize = i
	return &p
}

// WithLegendLabel returns a new HeatmapPrinter with a specific legend tag.
// This sets the Legend to true.
func (p HeatmapPrinter) WithLegendLabel(s string) *HeatmapPrinter {
	p.LegendLabel = s
	p.Legend = true
	return &p
}

// WithRGBRange returns a new HeatmapPrinter with a specific RGBRange.
func (p HeatmapPrinter) WithRGBRange(rgb ...RGB) *HeatmapPrinter {
	p.RGBRange = rgb
	return &p
}

// WithColors returns a new HeatmapPrinter with a specific Colors.
func (p HeatmapPrinter) WithColors(colors ...Color) *HeatmapPrinter {
	p.Colors = colors
	return &p
}

// WithWriter sets the Writer.
func (p HeatmapPrinter) WithWriter(writer io.Writer) *HeatmapPrinter {
	p.Writer = writer
	return &p
}

// Srender renders the HeatmapPrinter as a string.
func (p HeatmapPrinter) Srender() (string, error) {
	if err := p.errCheck(); err != nil {
		return "", err
	}

	if p.SeparatorStyle == nil {
		p.SeparatorStyle = DefaultHeatmap.SeparatorStyle
	}
	if p.AxisStyle == nil {
		p.AxisStyle = DefaultHeatmap.AxisStyle
	}

	if RawOutput {
		p.Legend = false
	}

	buffer := bytes.NewBufferString("")
	xAmount := len(p.Data[0]) - 1
	yAmount := len(p.Data) - 1
	p.minValue, p.maxValue = minMaxFloat32(p.Data)

	var data string
	for _, datum := range p.Data {
		for _, f := range datum {
			data += Sprintf("%v\n", f)
		}
	}

	if p.HasHeader {
		data, xAmount, yAmount = p.computeAxisData(data, xAmount, yAmount)
	}

	colWidth := internal.GetStringMaxWidth(data)
	legendColWidth := colWidth + 2

	if p.OnlyColoredCells && (p.CellSize > colWidth || !p.HasHeader) {
		colWidth = p.CellSize
	}

	if p.Boxed {
		p.renderSeparatorRow(buffer, colWidth, xAmount, true)
	}

	p.renderData(buffer, colWidth, xAmount, yAmount)

	if p.HasHeader {
		p.renderHeader(buffer, colWidth, xAmount)
	}

	if p.Boxed {
		p.renderSeparatorRow(buffer, colWidth, xAmount, false)
	}

	if p.Legend {
		p.renderLegend(buffer, legendColWidth)
	}

	buffer.WriteString("\n")

	return buffer.String(), nil
}

func (p HeatmapPrinter) computeAxisData(data string, xAmount, yAmount int) (string, int, int) {
	var header string
	for _, h := range p.Axis.XAxis {
		header += h + "\n"
	}
	for _, h := range p.Axis.YAxis {
		header += h + "\n"
	}

	if p.OnlyColoredCells {
		data = header
	} else {
		data += header
	}
	xAmount++
	yAmount++

	p.Axis.YAxis = append(p.Axis.YAxis, "")

	return data, xAmount, yAmount
}

func (p HeatmapPrinter) renderSeparatorRow(buffer *bytes.Buffer, colWidth, xAmount int, top bool) {
	tSep := p.TReverseSeparator
	rightSep := p.TopRightCornerSeparator
	leftSep := p.TopLeftCornerSeparator

	if top {
		tSep = p.TSeparator
		rightSep = p.BottomRightCornerSeparator
		leftSep = p.BottomLeftCornerSeparator
	} else {
		buffer.WriteString("\n")
	}
	buffer.WriteString(p.SeparatorStyle.Sprint(rightSep))
	for i := 0; i < xAmount+1; i++ {
		buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), colWidth))
		if i < xAmount {
			buffer.WriteString(p.SeparatorStyle.Sprint(tSep))
		}
	}
	buffer.WriteString(p.SeparatorStyle.Sprint(leftSep))

	if top {
		buffer.WriteString("\n")
	}
}

func (p HeatmapPrinter) renderLegend(buffer *bytes.Buffer, legendColWidth int) {
	buffer.WriteString("\n")
	buffer.WriteString("\n")
	if p.Boxed {
		p.boxLegend(buffer, p.LegendLabel, legendColWidth)
	} else {
		p.generateLegend(buffer, p.LegendLabel, legendColWidth)
	}
}

func (p HeatmapPrinter) renderHeader(buffer *bytes.Buffer, colWidth int, xAmount int) {
	buffer.WriteString("\n")
	if p.Boxed {
		buffer.WriteString(p.SeparatorStyle.Sprint(p.LSeparator))
	}
	if p.Grid {
		for i := 0; i < xAmount+1; i++ {
			buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), colWidth))
			if i < xAmount {
				buffer.WriteString(p.SeparatorStyle.Sprint(p.TCrossSeparator))
			}
		}
	}
	if p.Boxed {
		buffer.WriteString(p.SeparatorStyle.Sprint(p.LReverseSeparator))
	}
	if p.Grid {
		buffer.WriteString("\n")
	}
	for j, f := range p.Axis.XAxis {
		if j == 0 {
			if p.Boxed {
				buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator))
			}
			ct := internal.CenterText(" ", colWidth)
			if len(ct) < colWidth {
				ct += strings.Repeat(" ", colWidth-len(ct))
			}
			buffer.WriteString(p.AxisStyle.Sprint(ct))
			if p.Grid {
				buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator))
			}
		}
		var ct string
		ct = internal.CenterText(Sprintf("%v", f), colWidth)
		if len(ct) < colWidth {
			ct += strings.Repeat(" ", colWidth-len(ct))
		}
		buffer.WriteString(p.AxisStyle.Sprint(ct))

		if j < xAmount {
			if !p.Boxed && j == xAmount-1 {
				continue
			}
			if p.Grid {
				buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator))
			}
		}
	}
}

func (p HeatmapPrinter) renderData(buffer *bytes.Buffer, colWidth int, xAmount int, yAmount int) {
	for i, datum := range p.Data {
		if p.Boxed {
			buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator))
		}
		for j, f := range datum {
			if j == 0 && p.HasHeader {
				ct := internal.CenterText(p.Axis.YAxis[i], colWidth)
				if len(ct) < colWidth {
					ct += strings.Repeat(" ", colWidth-len(ct))
				}
				buffer.WriteString(p.AxisStyle.Sprint(ct))
				if p.Grid {
					buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator))
				}
			}
			var ct string
			if p.OnlyColoredCells {
				ct = internal.CenterText(" ", colWidth)
			} else {
				ct = internal.CenterText(Sprintf("%v", f), colWidth)
			}
			if len(ct) < colWidth {
				if len(Sprintf("%v", f)) == 1 {
					ct += strings.Repeat(" ", colWidth-len(ct))
				} else {
					ct = strings.Repeat(" ", colWidth-len(ct)) + ct
				}
			}
			if p.EnableRGB {
				rgb := p.RGBRange[0].Fade(p.minValue, p.maxValue, f, p.RGBRange[1:]...)
				rgbStyle := NewRGBStyle(p.TextRGB, rgb)
				if p.EnableComplementaryColor {
					complimentary := NewRGB(internal.Complementary(rgb.R, rgb.G, rgb.B))
					rgbStyle = NewRGBStyle(complimentary, rgb)
				}
				buffer.WriteString(rgbStyle.Sprint(ct))
			} else {
				color := getColor(p.minValue, p.maxValue, f, p.Colors...)
				fgColor := p.TextColor
				if p.EnableComplementaryColor {
					fgColor = complementaryColors[color]
				}
				buffer.WriteString(fgColor.Sprint(color.Sprintf(ct)))
			}
			if j < xAmount {
				if !p.Boxed && p.HasHeader && j == xAmount-1 {
					continue
				}
				if p.Grid {
					buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator))
				}
			}
			if p.Boxed && !p.HasHeader && j == xAmount {
				buffer.WriteString(p.SeparatorStyle.Sprint(p.VerticalSeparator))
			}
		}

		if i < yAmount {
			if p.HasHeader && i == yAmount-1 {
				continue
			}
			buffer.WriteString("\n")
			if p.Boxed {
				buffer.WriteString(p.SeparatorStyle.Sprint(p.LSeparator))
			}
			if p.Grid {
				for i := 0; i < xAmount+1; i++ {
					buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), colWidth))
					if i < xAmount {
						buffer.WriteString(p.SeparatorStyle.Sprint(p.TCrossSeparator))
					}
				}
			}
			if p.Boxed {
				buffer.WriteString(p.SeparatorStyle.Sprint(p.LReverseSeparator))
			}
			if p.Grid {
				buffer.WriteString("\n")
			}
		}
	}
}

func (p HeatmapPrinter) generateLegend(buffer *bytes.Buffer, legend string, legendColWidth int) {
	buffer.WriteString(p.AxisStyle.Sprint(legend))
	if p.Grid {
		buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator))
	} else {
		buffer.WriteString(" ")
	}
	if p.EnableRGB {
		p.generateRGBLegend(buffer, legendColWidth)
	} else {
		p.generateColorLegend(buffer, legendColWidth)
	}
}

func (p HeatmapPrinter) generateColorLegend(buffer *bytes.Buffer, legendColWidth int) {
	for i, color := range p.Colors {
		// the first color is the min value and the last color is the max value
		var f float32
		if i == 0 {
			f = p.minValue
		} else if i == len(p.Colors)-1 {
			f = p.maxValue
		} else {
			f = p.minValue + (p.maxValue-p.minValue)*float32(i)/float32(len(p.Colors)-1)
		}
		fgColor := p.TextColor
		if p.EnableComplementaryColor {
			fgColor = complementaryColors[color]
		}
		buffer.WriteString(fgColor.Sprint(color.Sprint(centerAndShorten(f, legendColWidth, p.LegendOnlyColoredCells))))
		if p.Grid && i < len(p.Colors)-1 && !p.LegendOnlyColoredCells {
			buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator))
		}
	}
}

func (p HeatmapPrinter) generateRGBLegend(buffer *bytes.Buffer, legendColWidth int) {
	p.rgbLegendValue = 10
	steps := len(p.RGBRange)
	if steps < p.rgbLegendValue {
		steps = p.rgbLegendValue
	}
	if p.LegendOnlyColoredCells {
		steps *= 3
	}
	for i := 0; i < steps; i++ {
		// the first color is the min value and the last color is the max value
		var f float32
		if i == 0 {
			f = p.minValue
		} else if i == steps-1 {
			f = p.maxValue
		} else {
			f = p.minValue + (p.maxValue-p.minValue)*float32(i)/float32(steps-1)
		}
		rgb := p.RGBRange[0].Fade(p.minValue, p.maxValue, f, p.RGBRange[1:]...)
		rgbStyle := NewRGBStyle(p.TextRGB, rgb)
		if p.EnableComplementaryColor {
			complimentary := NewRGB(internal.Complementary(rgb.R, rgb.G, rgb.B))
			rgbStyle = NewRGBStyle(complimentary, rgb)
		}
		if p.LegendOnlyColoredCells {
			buffer.WriteString(rgbStyle.Sprint(centerAndShorten(f, 1, p.LegendOnlyColoredCells)))
		} else {
			buffer.WriteString(rgbStyle.Sprint(centerAndShorten(f, legendColWidth, p.LegendOnlyColoredCells)))
		}
		if p.Grid && i < steps-1 && !p.LegendOnlyColoredCells {
			buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator))
		}
	}
}

func (p HeatmapPrinter) boxLegend(buffer *bytes.Buffer, legend string, legendColWidth int) {
	buffer.WriteString(p.SeparatorStyle.Sprint(p.BottomRightCornerSeparator))

	p.generateSeparatorRow(buffer, legend, legendColWidth, true)

	buffer.WriteString(p.SeparatorStyle.Sprint(p.BottomLeftCornerSeparator))
	buffer.WriteString("\n")
	buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator))

	p.generateLegend(buffer, legend, legendColWidth)

	buffer.WriteString(p.SeparatorStyle.Sprintf("%s", p.VerticalSeparator))
	buffer.WriteString("\n")

	buffer.WriteString(p.SeparatorStyle.Sprint(p.TopRightCornerSeparator))

	p.generateSeparatorRow(buffer, legend, legendColWidth, false)

	buffer.WriteString(p.SeparatorStyle.Sprint(p.TopLeftCornerSeparator))
}

func (p HeatmapPrinter) generateSeparatorRow(buffer *bytes.Buffer, legend string, legendColWidth int, top bool) {
	p.rgbLegendValue = 10
	steps := len(p.RGBRange)
	if steps < p.rgbLegendValue {
		steps = p.rgbLegendValue
	}
	if p.LegendOnlyColoredCells {
		steps *= 3
	}

	var xValue int
	if p.EnableRGB {
		xValue = len(p.RGBRange)
		if xValue < p.rgbLegendValue {
			xValue = p.rgbLegendValue
		}
	} else {
		xValue = len(p.Colors)
	}

	for i := 0; i < xValue+1; i++ {
		if i == 0 {
			firstLength := len(legend)
			buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), firstLength))
		} else {
			if p.LegendOnlyColoredCells {
				if p.EnableRGB {
					buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), steps/(xValue)))
				} else {
					buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), legendColWidth))
				}
			} else {
				buffer.WriteString(strings.Repeat(p.SeparatorStyle.Sprint(p.HorizontalSeparator), legendColWidth))
			}
		}
		if i < xValue && !p.LegendOnlyColoredCells || i == 0 {
			if top {
				buffer.WriteString(p.SeparatorStyle.Sprint(p.TSeparator))
			} else {
				buffer.WriteString(p.SeparatorStyle.Sprint(p.TReverseSeparator))
			}
		}
	}
}

func centerAndShorten(f float32, lineLength int, onlyColor bool) string {
	value := ""
	if !onlyColor {
		value = Sprintf("%.2v", f)
	}
	if len(value) > lineLength {
		value = value[:lineLength]
		if strings.HasSuffix(value, ".") {
			value = Sprintf("%.1v", f)
			lineLength = len(value)
		}
	}
	ct := internal.CenterText(value, lineLength)
	if len(ct) < lineLength {
		if len(Sprintf("%v", f)) == 1 {
			ct += strings.Repeat(" ", lineLength-len(ct))
		} else {
			ct = strings.Repeat(" ", lineLength-len(ct)) + ct
		}
	}

	return ct
}

func getColor(min float32, max float32, current float32, colors ...Color) Color {
	// split the range into equal parts
	// and assign a color to each part
	// the last color is assigned to the max value
	// and the first color to the min value
	// the rest of the colors are assigned to the
	// middle values
	step := (max - min) / float32(len(colors))
	for i := range colors {
		if current >= min+float32(i)*step && current < min+float32(i+1)*step {
			return colors[i]
		}
	}
	return colors[len(colors)-1]
}

// Render prints the HeatmapPrinter to the terminal.
func (p HeatmapPrinter) Render() error {
	s, err := p.Srender()
	if err != nil {
		return err
	}
	Fprintln(p.Writer, s)

	return nil
}

func (p HeatmapPrinter) errCheck() error {
	if p.HasHeader {
		if p.Axis.XAxis == nil {
			return errors.New("x axis is nil")
		}
		if p.Axis.YAxis == nil {
			return errors.New("y axis is nil")
		}

		if len(p.Axis.XAxis) == 0 {
			return errors.New("x axis is empty")
		}
		if len(p.Axis.YAxis) == 0 {
			return errors.New("y axis is empty")
		}

		for i := 1; i < len(p.Data); i++ {
			if len(p.Data[i]) != len(p.Axis.XAxis) {
				return errors.New("x axis length does not match data")
			}
		}
		if len(p.Axis.YAxis) != len(p.Data) {
			return errors.New("y axis length does not match data")
		}
	}

	if p.Data == nil {
		return errors.New("data is nil")
	}

	if len(p.Data) == 0 {
		return errors.New("data is empty")
	}

	// check if p.Data[n] has the same length
	for i := 1; i < len(p.Data); i++ {
		if len(p.Data[i]) != len(p.Data[0]) {
			return errors.New("data is not rectangular")
		}
	}

	return nil
}

// return min and max value of a slice
func minMaxFloat32(s [][]float32) (float32, float32) {
	var min, max float32
	min = math.MaxFloat32
	max = -math.MaxFloat32

	for _, r := range s {
		for _, c := range r {
			if c < min {
				min = c
			}
			if c > max {
				max = c
			}
		}
	}
	return min, max
}