Added cascading transparent annuation algorithm based on spans.

This commit is contained in:
Sascha L. Teichmann 2014-10-26 11:01:36 +01:00
parent eaa3949b10
commit 99ff99a8f4

View File

@ -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
}