Mercurial > gemma
changeset 3830:464a6a6e05e6 sld-colors
Clean color handling a bit.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 04 Jul 2019 11:03:36 +0200 |
parents | c1be6beafb54 |
children | 0ffea636d6b0 |
files | pkg/geoserver/templates.go pkg/geoserver/templates_test.go |
diffstat | 2 files changed, 37 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/geoserver/templates.go Thu Jul 04 10:37:42 2019 +0200 +++ b/pkg/geoserver/templates.go Thu Jul 04 11:03:36 2019 +0200 @@ -46,16 +46,6 @@ type colorClasses []colorClass -func round(c float64) uint8 { - c = math.Round(c) - if c < 0 { - c = 0 - } else if c > 255 { - c = 255 - } - return uint8(c) -} - func (cc colorClasses) interpolate(v float64) (color.RGBA, bool) { if len(cc) == 0 || v < cc[0].value || v > cc[len(cc)-1].value { return color.RGBA{}, false @@ -88,14 +78,21 @@ c := -m * v1 s := v*m + c - r := (1-s)*float64(cc[i].color.R) + s*float64(cc[i+1].color.R) - g := (1-s)*float64(cc[i].color.G) + s*float64(cc[i+1].color.G) - b := (1-s)*float64(cc[i].color.B) + s*float64(cc[i+1].color.B) + interpolate := func(a, b uint8) uint8 { + v := math.Round(float64(a) + (float64(b)-float64(a))*s) + if v < 0 { + return 0 + } + if v > 255 { + return 255 + } + return uint8(v) + } return color.RGBA{ - R: round(r), - G: round(g), - B: round(b), + R: interpolate(cc[i].color.R, cc[i+1].color.R), + G: interpolate(cc[i].color.G, cc[i+1].color.G), + B: interpolate(cc[i].color.B, cc[i+1].color.B), A: 0xff, }, true } @@ -210,26 +207,37 @@ HasHigh bool Low float64 HasLow bool - Color string + color color.RGBA +} + +func (cb *classBreak) Color() string { + return fmt.Sprintf("#%02x%02x%02x", + cb.color.R, + cb.color.G, + cb.color.B, + ) } func (cc colorClasses) toClassBreaks() []classBreak { - cbs := make([]classBreak, len(cc)) + cbs := make([]classBreak, len(cc), len(cc)+1) for i := range cc { if i > 0 { cbs[i].Low = cc[i-1].value cbs[i].HasLow = true } - if i < len(cc)-1 { - cbs[i].High = cc[i].value - cbs[i].HasHigh = true - } - cbs[i].Color = fmt.Sprintf("#%02x%02x%02x", - cc[i].color.R, - cc[i].color.G, - cc[i].color.B) + cbs[i].High = cc[i].value + cbs[i].HasHigh = true + cbs[i].color = cc[i].color } + if len(cc) > 0 { + cbs = append(cbs, classBreak{ + color: cc[len(cc)-1].color, + Low: cc[len(cc)-1].value, + HasLow: true, + }) + } + return cbs }
--- a/pkg/geoserver/templates_test.go Thu Jul 04 10:37:42 2019 +0200 +++ b/pkg/geoserver/templates_test.go Thu Jul 04 11:03:36 2019 +0200 @@ -895,7 +895,9 @@ </StyledLayerDescriptor> ` -const classBreaksConfig = `1:#ff00dd,1.5,1.7,1.9,2.1,2.3,2.5:#f25f20,2.7,2.9,3.1,3.3,3.5,4:#8ad51a,4.5,5,5.5,6,6.5,7,7.5:#1414ff` +const classBreaksConfig = `1:#ff00dd,1.5,1.7,1.9,2.1,2.3,` + + `2.5:#f25f20,2.7,2.9,3.1,3.3,3.5,` + + `4:#8ad51a,4.5,5,5.5,6,6.5,7.5:#1414ff` func TestTemplate(t *testing.T) {