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

Switch to Colors package (do not merge yet) #137

Closed
wants to merge 5 commits into from
Closed
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 REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
julia 0.3
Color
Colors
DataStructures
Iterators 0.1.5
JSON
Expand Down
10 changes: 3 additions & 7 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ order: 1


```{.julia hide="true"}
using Compose, Color
using Compose, Colors

function drawlogo()
color1 = LCHab(68, 74, 192)
Expand Down Expand Up @@ -238,10 +238,10 @@ circle(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray)
```

The first of these creates only circle centered at `(x, y)` with radius `r`. The
second form can succinctly create many circles (using the [Color](https://github.com/JuliaLang/Color.jl) package to specify the `LHCab` colorspace):
second form can succinctly create many circles (using the [Colors](https://github.com/JuliaGraphics/Colors.jl) package to specify the `LHCab` colorspace):

```julia
using Color
using Colors
compose(context(),
circle([0.25, 0.5, 0.75], [0.25, 0.5, 0.75], [0.1, 0.1, 0.1]),
fill(LCHab(92, 10, 77)))
Expand Down Expand Up @@ -305,7 +305,3 @@ compose(sierpinski(6), fill(LCHab(92, 10, 77)))
There are no safeguards to check for cycles. You can produce a graph with a
cycle and Compose will run in an infinite loop trying to draw it. In most
applications, this isn't a concern.




4 changes: 2 additions & 2 deletions examples/dashedlines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Draw lines with various dash styles.

using Compose
using Color
using Colors

function draw_lines(dash_patterns)
if length(dash_patterns) == 0
Expand All @@ -30,4 +30,4 @@ patterns = Array[
c = draw_lines(patterns)

img = PDF("dash.pdf", 4inch, 4(sqrt(3)/2)inch)
draw(img, compose(c, stroke(color("black")), linewidth(1mm)))
draw(img, compose(c, stroke(parse(Color, "black")), linewidth(1mm)))
2 changes: 1 addition & 1 deletion examples/golden_rect.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env julia

using Color
using Colors
using Compose

function golden_rect(n::Int)
Expand Down
4 changes: 2 additions & 2 deletions examples/linecaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Draw lines with various dash styles.

using Compose
using Color
using Colors

function draw_lines(caps)
if length(caps) == 0
Expand All @@ -20,5 +20,5 @@ c = draw_lines(caps)
imgs = [SVG("linecaps.svg", 2cm, 3cm),
PDF("linecaps.pdf", 2cm, 3cm)]
for img = imgs
draw(img, compose(c, stroke(color("black")), linewidth(2mm)))
draw(img, compose(c, stroke(parse(Color, "black")), linewidth(2mm)))
end
4 changes: 2 additions & 2 deletions examples/linejoins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Draw lines with various dash styles.

using Compose
using Color
using Colors

function draw_lines(joins)
if length(joins) == 0
Expand All @@ -21,5 +21,5 @@ imgs = [SVG("linejoins.svg", 2cm, 3cm),
PDF("linejoins.pdf", 2cm, 3cm),
SVGJS("linejoins.js", 2cm, 3cm)]
for img = imgs
draw(img, compose(c, stroke(color("black")), linewidth(2mm), fill(nothing)))
draw(img, compose(c, stroke(parse(Color, "black")), linewidth(2mm), fill(nothing)))
end
12 changes: 7 additions & 5 deletions src/Compose.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Compose

using Color
using Colors
using Iterators
using DataStructures
using Compat
Expand All @@ -20,7 +20,7 @@ export compose, compose!, Context, UnitBox, AbsoluteBoundingBox, Rotation, Mirro
vbottom, SVG, SVGJS, PGF, PNG, PS, PDF, draw, pad, pad_inner, pad_outer,
hstack, vstack, gridstack, LineCapButt, LineCapSquare, LineCapRound,
CAIROSURFACE, introspect, set_default_graphic_size, set_default_jsmode,
boundingbox, Patchable
boundingbox, Patchable, color

abstract Backend

Expand Down Expand Up @@ -124,7 +124,7 @@ default_font_family = "Helvetica Neue,Helvetica,Arial,sans"
default_font_size = 11pt
default_line_width = 0.3mm
default_stroke_color = nothing
default_fill_color = color("black")
default_fill_color = parse(Color, "black")


# Use cairo for the PNG, PS, PDF if it's installed.
Expand Down Expand Up @@ -299,6 +299,8 @@ end

const pad = pad_outer

end # module Compose

# Coerce to a Color
color(c::Color) = c
color(str::AbstractString) = parse(Color, str)

end # module Compose
12 changes: 6 additions & 6 deletions src/cairo_backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,12 @@ end


function apply_property(img::Image, p::FillOpacityPrimitive)
img.fill = RGBA{Float64}(img.fill.c, p.value)
img.fill = RGBA{Float64}(opaquecolor(img.fill), p.value)
end


function apply_property(img::Image, p::StrokeOpacityPrimitive)
img.stroke = RGBA{Float64}(img.stroke.c, p.value)
img.stroke = RGBA{Float64}(opaquecolor(img.stroke), p.value)
end


Expand Down Expand Up @@ -695,7 +695,7 @@ function fillstroke(img::Image, strokeonly::Bool=false)
end

if img.fill.alpha > 0.0 && !strokeonly
Cairo.set_source_rgba(img.ctx, img.fill.c.r, img.fill.c.g, img.fill.c.b,
Cairo.set_source_rgba(img.ctx, img.fill.r, img.fill.g, img.fill.b,
img.fill.alpha)

if img.stroke.alpha > 0.0
Expand All @@ -706,8 +706,8 @@ function fillstroke(img::Image, strokeonly::Bool=false)
end

if img.stroke.alpha > 0.0
Cairo.set_source_rgba(img.ctx, img.stroke.c.r, img.stroke.c.g,
img.stroke.c.b, img.stroke.alpha)
Cairo.set_source_rgba(img.ctx, img.stroke.r, img.stroke.g,
img.stroke.b, img.stroke.alpha)
Cairo.set_dash(img.ctx, img.stroke_dash)
Cairo.set_line_cap(img.ctx, cairo_linecap(img.stroke_linecap))
Cairo.set_line_join(img.ctx, cairo_linejoin(img.stroke_linejoin))
Expand Down Expand Up @@ -857,7 +857,7 @@ function draw(img::Image, prim::TextPrimitive)
end
end

Cairo.set_source_rgba(img.ctx, img.fill.c.r, img.fill.c.g, img.fill.c.b,
Cairo.set_source_rgba(img.ctx, img.fill.r, img.fill.g, img.fill.b,
img.fill.alpha)

if prim.rot.theta != 0.0
Expand Down
10 changes: 5 additions & 5 deletions src/patchable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,10 @@ draw(img::Patchable, prim::FillOpacityPrimitive) =
:opacity, prim.value

function draw!(img::Patchable, prim::FillPrimitive, props)
if isa(prim.color, AlphaColorValue)
props[:fill] = svg_fmt_color(prim.color.c)
if isa(prim.color, TransparentColor)
props[:fill] = svg_fmt_color(opaquecolor(prim.color))
props["fill-opacity"] = prim.color.alpha
elseif isa(prim.color, ColorValue)
elseif isa(prim.color, OpaqueColor)
props[:fill] = svg_fmt_color(prim.color)
else
props[:fill] = "none"
Expand Down Expand Up @@ -352,8 +352,8 @@ draw(img::Patchable, prim::StrokeOpacityPrimitive) =
"stroke-opacity", prim.value

function draw!(img::Patchable, prim::StrokePrimitive, dict)
if isa(prim.color, AlphaColorValue)
dict[:stroke] = svg_fmt_color(prim.color.c)
if isa(prim.color, TransparentColor)
dict[:stroke] = svg_fmt_color(opaquecolor(prim.color))
dict["stroke-opacity"] = prim.color.alpha
else
dict[:stroke] = svg_fmt_color(prim.color)
Expand Down
16 changes: 8 additions & 8 deletions src/pgf_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ type PGF <: Backend
# Fill properties cannot be "cleanly" applied to
# multiple form primitives. It must be applied
# each time an object is drawn
fill::Union(Nothing, ColorValue)
fill::Union(Nothing, OpaqueColor)
fill_opacity::Float64

stroke::Union(Nothing, ColorValue)
stroke::Union(Nothing, OpaqueColor)
stroke_opacity::Float64

fontfamily::Union(Nothing,String)
Expand All @@ -44,7 +44,7 @@ type PGF <: Backend
# a draw parameter. Whenever we encounter a color, we add it to the
# color_set set. That way, we can write out all the color
# definitions at the same time.
color_set::Set{ColorValue}
color_set::Set{OpaqueColor}

# Stack of property frames (groups of properties) currently in effect.
property_stack::Vector{PGFPropertyFrame}
Expand Down Expand Up @@ -98,7 +98,7 @@ type PGF <: Backend
img.fontsize = 12.0
img.indentation = 0
img.out = out
img.color_set = Set{ColorValue}([color("black")])
img.color_set = Set{OpaqueColor}([parse(Color, "black")])
img.property_stack = Array(PGFPropertyFrame, 0)
img.vector_properties = Dict{Type, Union(Nothing, Property)}()
# img.clippaths = Dict{ClipPrimitive, String}()
Expand Down Expand Up @@ -277,8 +277,8 @@ function push_property!(props_str, img::PGF, property::StrokeDashPrimitive)
end

function push_property!(props_str, img::PGF, property::StrokePrimitive)
if isa(property.color, AlphaColorValue)
img.stroke = property.color.c
if isa(property.color, TransparentColor)
img.stroke = opaquecolor(property.color)
img.stroke_opacity = property.color.alpha
else
img.stroke = property.color
Expand All @@ -290,8 +290,8 @@ function push_property!(props_str, img::PGF, property::StrokePrimitive)
end

function push_property!(props_str, img::PGF, property::FillPrimitive)
if isa(property.color, AlphaColorValue)
img.fill = property.color.c
if isa(property.color, TransparentColor)
img.fill = opaquecolor(property.color)
img.fill_opacity = property.color.alpha
else
img.fill = property.color
Expand Down
4 changes: 2 additions & 2 deletions src/property.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function stroke(c::Nothing)
end


function stroke(c::Union(ColorValue, AlphaColorValue, String))
function stroke(c::Union(OpaqueColor, TransparentColor, String))
return Stroke([StrokePrimitive(color(c))])
end

Expand All @@ -79,7 +79,7 @@ function fill(c::Nothing)
end


function fill(c::Union(ColorValue, AlphaColorValue, String))
function fill(c::Union(OpaqueColor, TransparentColor, String))
return Fill([FillPrimitive(color(c))])
end

Expand Down
13 changes: 6 additions & 7 deletions src/svg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const xmlns = Dict()


# Format a floating point number into a decimal string of reasonable precision.
function svg_fmt_float(x::FloatingPoint)
function svg_fmt_float(x::Fractional)
# All svg (in our use) coordinates are in millimeters. This number gives the
# largest deviation from the true position allowed in millimeters.
const eps = 0.01
Expand Down Expand Up @@ -85,7 +85,7 @@ end
end

# Format a color for SVG.
svg_fmt_color(c::ColorValue) = string("#", hex(c))
svg_fmt_color(c::OpaqueColor) = string("#", hex(c))
svg_fmt_color(c::Nothing) = "none"


Expand Down Expand Up @@ -572,19 +572,19 @@ end
function print_property(img::SVG, property::StrokePrimitive)
if property.color.alpha != 1.0
@printf(img.out, " stroke=\"%s\" stroke-opacity=\"%0.3f\"",
svg_fmt_color(property.color.c), property.color.alpha)
svg_fmt_color(opaquecolor(property.color)), property.color.alpha)
else
@printf(img.out, " stroke=\"%s\"", svg_fmt_color(property.color.c))
@printf(img.out, " stroke=\"%s\"", svg_fmt_color(opaquecolor(property.color)))
end
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(property.color.c), property.color.alpha)
svg_fmt_color(opaquecolor(property.color)), property.color.alpha)
else
@printf(img.out, " fill=\"%s\"", svg_fmt_color(property.color.c))
@printf(img.out, " fill=\"%s\"", svg_fmt_color(opaquecolor(property.color)))
end
end

Expand Down Expand Up @@ -1249,4 +1249,3 @@ function pop_property_frame(img::SVG)
end
end
end

21 changes: 18 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ end
# Compare with cached output
cachedout = joinpath(testdir, "data")
differentfiles = String[]
const creator_producer = r"(Creator|Producer)"
for output in readdir(cachedout)
cached = open(readall, joinpath(cachedout, output))
genned = open(readall, joinpath(testdir, output))
if cached != genned
cached = open(readlines, joinpath(cachedout, output))
genned = open(readlines, joinpath(testdir, output))
same = (n=length(cached)) == length(genned)
if same
lsame = Bool[cached[i] == genned[i] for i = 1:n]
if !all(lsame)
for idx in find(!lsame)
# Don't worry about lines that are due to
# Creator/Producer (e.g., Cairo versions)
if !isempty(search(cached[idx], creator_producer))
lsame[idx] = true
end
end
end
same = same & all(lsame)
end
if !same
push!(differentfiles, output)
else #Delete generated file
rm(joinpath(testdir, output))
Expand Down