Skip to content

Commit

Permalink
Merge pull request #332 from Mattriks/svg_alpha_test
Browse files Browse the repository at this point in the history
svg_alpha_test
  • Loading branch information
bjarthur authored Dec 3, 2018
2 parents 9e1682b + dc32a8f commit 84afc6c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
19 changes: 10 additions & 9 deletions docs/src/gallery/transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ Author = ["Mattriks"]
using Compose
set_default_graphic_size(15cm,5cm)
set_default_graphic_size(15cm,5cm)
f_points = [(.1, .1), (.9, .1), (.9, .2), (.2, .2), (.2, .4), (.6, .4), (.6, .5),
(.2, .5), (.2, .9), (.1, .9), (.1, .1)]
f_points = (x->0.4.*x.+0.1).(f_points)
fpoly(ϕ::Float64) = (context(rotation=Rotation(ϕ,0.3,0.46)), polygon(f_points))
imgf(θ, ϕ=0.0, x=0.5,y=0.5) = compose(context(), fill("salmon"), fpoly(ϕ),
imgfa(θ, ϕ=0.0, x=0.5,y=0.5) = compose(context(),
fill("salmon"), fillopacity(1.0), fpoly(ϕ),
(context(rotation=Rotation(θ,x,y)), line([(x-0.5,y),(x+0.5,y)]), circle(x,y, 0.02)),
(context(mirror=Mirror(θ, x, y)), fpoly(ϕ))
)
F = hstack(imgf(-π/4), imgf(-π/2.2), imgf(π/4, 1π))
img = compose(context(), rectangle(), fill(nothing), stroke("black"), F)
Fmir = hstack(imgfa(-π/4), imgfa(-π/2.2), imgfa(π/4, 1π))
img = compose(context(), rectangle(), fill(nothing), stroke("black"), Fmir)
```

## [`Rotation`](@ref)
Expand All @@ -37,16 +37,17 @@ f_points = [(.1, .1), (.9, .1), (.9, .2), (.2, .2), (.2, .4),
(.6, .4), (.6, .5), (.2, .5), (.2, .9), (.1, .9), (.1, .1)]
rect(c::String) = (context(), rectangle(), stroke(c))
circ(c::String, s::Float64=0.4) = (context(), circle([x],[y],[0.03,s]), stroke(c))
fpoly(c::String) = (context(), polygon(f_points), fill(c))
fpoly(c::String) = (context(), polygon(f_points), fill(c), fillopacity(1.0))
contextC(θ::Float64) = (context(0.5,0.5,1.5,1.5, units=UnitBox(0,0,1,1),
rotation=Rotation(θ,x,y)), fpoly("steelblue"), circ("orange"))
imgf(θ::Float64) = compose(context(), fill(nothing), rect("black"),
(context(0.15, 0.15, 0.7, 0.7, units=UnitBox(0,0,2,2)), rect("red"),
imgf(θ::Float64) = compose(context(),
(context(0.15, 0.15, 0.7, 0.7, units=UnitBox(0,0,2,2)), rect("red"),
contextC(θ)) # context C in context B in context A
)
)
x, y, θ = 0.5, 0.25, π/3
hstack(imgf(-θ), imgf(0.), imgf(θ))
Frot = hstack(imgf(-θ), imgf(0.), imgf(θ))
img = compose(context(), rectangle(), fill(nothing), stroke("black"), Frot)
```


2 changes: 1 addition & 1 deletion src/property.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const FillOpacity = Property{FillOpacityPrimitive}
"""
fillopacity(value)
Define a fill opacity, where 0≤value≤1.
Define a fill opacity, where 0≤value≤1. For svg, nested contexts will inherit from parent contexts e.g. `(context(), fillopacity(a), (context(), fill(c::String), circle()))`.
"""
fillopacity(value::Float64) = FillOpacity([FillOpacityPrimitive(value)])

Expand Down
14 changes: 12 additions & 2 deletions src/svg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ clippathurl(img::SVG, property::ClipPrimitive) = get!(() -> genid(img), img.clip

function push_property_frame(img::SVG, properties::Vector{Property})
isempty(properties) && return
svgalphatest(properties)

frame = SVGPropertyFrame()
applied_properties = Set{Type}()
Expand Down Expand Up @@ -1257,8 +1258,17 @@ end



# Currently for svg, you can't use a transparent fill(color) and fillopacity() together,
# so throw a warning if a user tries to do that.



function svgalphatest(properties::Vector{Property})
has_fill_opacity = any(isa.(properties, Property{FillOpacityPrimitive}))
!has_fill_opacity && return
is_fill = isa.(properties, Property{FillPrimitive})
!any(is_fill) && return
fillproperties = properties[is_fill][1]
has_alpha = any([alpha(x.color) for x in fillproperties.primitives].<1.0)
has_alpha && @warn "For svg transparent colors, use either e.g. fill(RGBA(r,g,b,a)) or fillopacity(a), but not both."
end


0 comments on commit 84afc6c

Please sign in to comment.