Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade svg_fmt_color to svg rgba spec #319

Merged
merged 1 commit into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. Note for svg alphas multiply: e.g. using `fill(RGBA(r,g,b,a₁))` and `fillopacity(a₂)` together will result in an opacity=a₁*a₂.
Mattriks marked this conversation as resolved.
Show resolved Hide resolved
"""
fillopacity(value::Float64) = FillOpacity([FillOpacityPrimitive(value)])

Expand Down
17 changes: 6 additions & 11 deletions src/svg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ end
# Format a color for SVG.
svg_fmt_color(c::Color) = string("#", hex(c))
svg_fmt_color(c::Nothing) = "none"
svg_fmt_color(c::CT) where CT<:TransparentColor =
string("rgba(", join(floor.(Int,[c.r,c.g,c.b].*255),","), ",", svg_fmt_float(c.alpha),")")


# Replace newlines in a string with the appropriate SVG tspan tags.
function svg_newlines(input::AbstractString, x::Float64)
Expand Down Expand Up @@ -591,12 +594,7 @@ function print_property(img::SVG, property::StrokePrimitive)
end

function print_property(img::SVG, property::FillPrimitive)
if property.color.alpha != 1.0
@printf(img.out, " fill=\"%s\" fill-opacity=\"%0.3f\"",
svg_fmt_color(color(property.color)), property.color.alpha)
else
@printf(img.out, " fill=\"%s\"", svg_fmt_color(color(property.color)))
end
@printf(img.out, " fill=\"%s\"", svg_fmt_color(property.color))
end

function print_property(img::SVG, property::StrokeDashPrimitive)
Expand Down Expand Up @@ -698,7 +696,6 @@ function print_vector_properties(img::SVG, idx::Int, supress_fill::Bool=false)
end

has_stroke_opacity = haskey(img.vector_properties, StrokeOpacity)
has_fill_opacity = haskey(img.vector_properties, FillOpacity)

for (propertytype, property) in img.vector_properties
if property === nothing ||
Expand All @@ -709,10 +706,8 @@ function print_vector_properties(img::SVG, idx::Int, supress_fill::Bool=false)
idx > length(property.primitives) &&
error("Vector form and vector property differ in length. Can't distribute.")

# let the opacity primitives clobber the alpha value in fill and stroke
if propertytype == Fill && has_fill_opacity
print_property(img, FillPrimitive(RGBA{Float64}(color(property.primitives[idx].color), 1.0)))
elseif propertytype == Stroke && has_stroke_opacity
# let the opacity primitive clobber the alpha value in stroke
if propertytype == Stroke && has_stroke_opacity
print_property(img, StrokePrimitive(RGBA{Float64}(color(property.primitives[idx].color), 1.0)))
else
print_property(img, property.primitives[idx])
Expand Down
5 changes: 4 additions & 1 deletion test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ end
properties = [fill(["red","blue"]), fillopacity(0.3), stroke("black")]
img1 = PNG(); Compose.push_property_frame(img1, properties)
img2 = SVG(); Compose.push_property_frame(img2, properties)
Compose.print_vector_properties.([img2], [1,2])
a = String(img2.out.data)
@test getfield.(img1.vector_properties[Compose.Property{Compose.FillOpacityPrimitive}].primitives, :value) == [0.3, 0.3]
@test occursin("fill-opacity=\"0.3\"", String(img2.out.data))
@test occursin("fill-opacity=\"0.3\"", a)
@test all(occursin.(["fill=\"rgba(255,0,0,1)\"","fill=\"rgba(0,0,255,1)\""], a))
end