Skip to content

Commit

Permalink
finished issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jakopako committed Jan 2, 2023
1 parent fddf0d6 commit 62b8942
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 57 deletions.
66 changes: 9 additions & 57 deletions automate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package automate
import (
"errors"
"fmt"
"math"
"sort"
"strings"

Expand All @@ -29,61 +28,22 @@ type locationProps struct {
type locationManager []*locationProps

func (l locationManager) setColors() {
// todo this function should set the color depending on the proximity to other fields
if len(l) == 0 {
return
}
for i, e := range l {
if i != 0 {
e.distance = l[i-1].distance + distance(l[i-1].loc, e.loc)
}
if i%2 == 0 {
e.color = tcell.NewRGBColor(0, 0, 0)
} else {
e.color = tcell.NewRGBColor(100, 100, 100)
}
}
// scale to 1 and map to rgb
maxDist := l[len(l)-1].distance * 1.2
s := 0.73
v := 0.96
for _, e := range l {
e.distance = e.distance / maxDist
// from https://go.dev/play/p/9q5yBNDh3W
var r, g, b float64
h := e.distance * 6
i := math.Floor(h)
v1 := v * (1 - s)
v2 := v * (1 - s*(h-i))
v3 := v * (1 - s*(1-(h-i)))

if i == 0 {
r = v
g = v3
b = v1
} else if i == 1 {
r = v2
g = v
b = v1
} else if i == 2 {
r = v1
g = v
b = v3
} else if i == 3 {
r = v1
g = v2
b = v
} else if i == 4 {
r = v3
g = v1
b = v
} else {
r = v
g = v1
b = v2
}

r = r * 255 //RGB results from 0 to 255
g = g * 255
b = b * 255
e.color = tcell.NewRGBColor(int32(r), int32(g), int32(b))
h := e.distance / maxDist
r, g, b := utils.HSVToRGB(h, s, v)
e.color = tcell.NewRGBColor(r, g, b)
}
}

Expand Down Expand Up @@ -397,10 +357,6 @@ parse:
locMan.setColors()

if len(locMan) > 0 {
// sort.Slice(locMan, func(p, q int) bool {
// return locMan[p].loc.Selector > locMan[q].loc.Selector
// })

selectFieldsTable(locMan)

var fs []scraper.ElementLocation
Expand All @@ -421,10 +377,7 @@ parse:

func selectFieldsTable(locMan locationManager) {
app := tview.NewApplication()
// table := tview.NewTable().SetBorders(true)
table := tview.NewTable()
// table.SetBorderPadding(0, 0, 0, 0)
// table.SetBorder(true)
table := tview.NewTable().SetBorders(true)
cols, rows := 5, len(locMan)+1
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
Expand Down Expand Up @@ -453,8 +406,7 @@ func selectFieldsTable(locMan locationManager) {
}
table.SetCell(r, c,
tview.NewTableCell(ss).
SetTextColor(color).
SetBackgroundColor(locMan[r-1].color).
SetTextColor(locMan[r-1].color).
SetAlign(tview.AlignCenter))
}
}
Expand All @@ -477,7 +429,7 @@ func selectFieldsTable(locMan locationManager) {
} else {
table.GetCell(row, 0).SetTextColor(tcell.ColorGreen)
for i := 1; i < 5; i++ {
table.GetCell(row, i).SetTextColor(tcell.ColorWhite)
table.GetCell(row, i).SetTextColor(locMan[row-1].color)
}
}
})
Expand Down
42 changes: 42 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"math"
)

func ShortenString(s string, l int) string {
Expand All @@ -10,3 +11,44 @@ func ShortenString(s string, l int) string {
}
return s
}

func HSVToRGB(h, s, v float64) (int32, int32, int32) {
// from https://go.dev/play/p/9q5yBNDh3W
var r, g, b float64
h = h * 6
i := math.Floor(h)
v1 := v * (1 - s)
v2 := v * (1 - s*(h-i))
v3 := v * (1 - s*(1-(h-i)))

if i == 0 {
r = v
g = v3
b = v1
} else if i == 1 {
r = v2
g = v
b = v1
} else if i == 2 {
r = v1
g = v
b = v3
} else if i == 3 {
r = v1
g = v2
b = v
} else if i == 4 {
r = v3
g = v1
b = v
} else {
r = v
g = v1
b = v2
}

r = r * 255 //RGB results from 0 to 255
g = g * 255
b = b * 255
return int32(r), int32(g), int32(b)
}

0 comments on commit 62b8942

Please sign in to comment.