srwiley / rasterx

Rasterx is an SVG 2.0 path compliant rasterizer that can use either scany, the golang vector or a derivative of the freetype anti-aliaser.
BSD 3-Clause "New" or "Revised" License
132 stars 11 forks source link

gradient stops uses pointer slice so you can easily modify values, an… #2

Closed rcoreilly closed 6 years ago

rcoreilly commented 6 years ago

…d added SetBounds convenience for setting bounds from image.Rectangle

Thanks! :)

rcoreilly commented 6 years ago

actually, I just realized that I had made a stupid mistake -- please disregard that pull request -- the non-pointer version is simpler and better for this case. (at least I figured out how to do a pull request -- starting from a fork instead of your repo was the key missing step..)

the SetBounds method would still be handy.

Also, you might want to add this method -- can't guarantee that it implements 100% of the spec but at least handles most of the basic example cases..

// FixGradientStops applies the CSS rules to regularize the gradient stops: https://www.w3.org/TR/css3-images/#color-stop-syntax
func FixGradientStops(grad *rasterx.Gradient) {
    sz := len(grad.Stops)
    if sz == 0 {
        return
    }
    splitSt := -1
    last := 0.0
    for i := 0; i < sz; i++ {
        st := &(grad.Stops[i])
        if i == sz-1 && st.Offset == 0 {
            if last < 1.0 {
                st.Offset = 1.0
            } else {
                st.Offset = last
            }
        }
        if i > 0 && st.Offset == 0 && splitSt < 0 {
            splitSt = i
            st.Offset = last
            continue
        }
        if splitSt > 0 {
            start := grad.Stops[splitSt].Offset
            end := st.Offset
            per := (end - start) / float64(1+(i-splitSt))
            cur := start + per
            for j := splitSt; j < i; j++ {
                grad.Stops[j].Offset = cur
                cur += per
            }
        }
        last = st.Offset
    }
    // fmt.Printf("grad stops:\n")
    // for i := 0; i < sz; i++ {
    //  st := grad.Stops[i]
    //  fmt.Printf("%v\t%v opacity: %v\n", i, st.Offset, st.Opacity)
    // }
}
srwiley commented 6 years ago

Yes, I agree, i will close both of these since I did not incorporate the first. For Future reference there is a way to cancel pull requests. I had to do it once ;-)