From 99ff99a8f42e7f3d1eefb6f260f88f16f7665d61 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Sun, 26 Oct 2014 11:01:36 +0100 Subject: [PATCH] Added cascading transparent annuation algorithm based on spans. --- common/colors.go | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/common/colors.go b/common/colors.go index caeb984..eaf1b3f 100644 --- a/common/colors.go +++ b/common/colors.go @@ -24,10 +24,6 @@ type namedColor struct { color color.RGBA } -func (colors *Colors) IsTransparent(index int32) bool { - return index < colors.NumTransparent -} - type sortByAlpha []namedColor func (colors sortByAlpha) Less(i, j int) bool { @@ -88,3 +84,40 @@ func ParseColors(filename string) (colors *Colors, err error) { NumTransparent: numTransparent} return } + +func (colors *Colors) IsTransparent(index int32) bool { + return index < colors.NumTransparent +} + +func min(a, b int32) int32 { + if a < b { + return a + } + return b +} + +func BlendColor(c1, c2 color.RGBA, a float32) color.RGBA { + b := float32(1) - a + return color.RGBA{ + R: uint8(float32(c1.R)*a + float32(c2.R)*b), + G: uint8(float32(c1.G)*a + float32(c2.G)*b), + B: uint8(float32(c1.B)*a + float32(c2.B)*b), + A: 0xff} +} + +func (colors *Colors) BlendColors(span *Span, col color.RGBA, pos int32) color.RGBA { + curr := span + // Ignore colors below pos. + for ; curr != nil && pos >= curr.To; curr = curr.Next { + } + if curr == nil { + return col + } + const scale = float32(1) / float32(100) + for ; curr != nil; curr = curr.Next { + // At least 20% attenuation + 5% extra for each depth meter. + factor := float32(min(100, 20+(curr.To-curr.From)*5)) * scale + col = BlendColor(colors.Colors[curr.Value], col, factor) + } + return col +}