diff --git a/CHANGELOG.md b/CHANGELOG.md index 247b3bc3..e40387f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## [v1.1.2] - 2018-10-31 + +### Added + +### Changed + +- use eachindex more +- docs use more svg images +- changed preview() in Windows again + +### Removed + +### Deprecated + ## [v1.1.1] - 2018-10-04 ### Added diff --git a/docs/src/assets/examples/sector-chart.jl b/docs/src/assets/examples/sector-chart.jl index 1d16c303..dd43303b 100644 --- a/docs/src/assets/examples/sector-chart.jl +++ b/docs/src/assets/examples/sector-chart.jl @@ -85,7 +85,7 @@ function sectorchart(centerpos, innerradius, tilewidth, tileheight, rawdatavalue glyph_x_bearing, glyph_y_bearing, glyph_width, glyph_height, glyph_x_advance, glyph_y_advance = textextents(labels[i]) shiftangle = asin((glyph_width/2)/innerradius) - textcurve(labels[i], rotangle-shiftangle, innerradius - textoffset) + textcurve(titlecase(labels[i]), rotangle-shiftangle, innerradius - textoffset) # show the original raw data value (not the rescaled value used for plotting) # refpos is for text placement @@ -113,7 +113,7 @@ end function main() fname = "/tmp/sector-chart.svg" - width, height = 1064, 1064 + width, height = 1064, 1200 Drawing(width, height, fname) origin() background("ivory") @@ -144,7 +144,7 @@ function main() "sienna", # Rust ] - languagecolors = Dict(languages[i] => cols[i] for i in 1:length(languages)) + languagecolors = Dict(languages[i] => cols[i] for i in eachindex(languages)) # how many charts are we plotting? # numberofrows, numberofcolumns = howmanyrowscolumns(length(benchmarknames)) diff --git a/docs/src/colors-styles.md b/docs/src/colors-styles.md index c7dfd901..cab33455 100644 --- a/docs/src/colors-styles.md +++ b/docs/src/colors-styles.md @@ -17,31 +17,37 @@ For color definitions and conversions, you can use Colors.jl. The difference between the `setcolor()` and `sethue()` functions is that `sethue()` is independent of alpha opacity, so you can change the hue without changing the current opacity value. -Named colors, such as "gold", or "lavender", can be found in Colors.color_names. This code shows the first 625 colors. +Named colors, such as "gold", or "lavender", can be found in Colors.color_names. ```@example using Luxor, Colors # hide -Drawing(800, 500, "assets/figures/colors.png") # hide +Drawing(800, 800, "assets/figures/colors.svg") # hide + origin() # hide background("white") # hide -fontsize(5) # hide -cols = collect(Colors.color_names) -tiles = Tiler(800, 500, 25, 25) -for (pos, n) in tiles - sethue(cols[n][1]) - box(pos, tiles.tilewidth, tiles.tileheight, :fill) - clab = convert(Lab, parse(Colorant, cols[n][1])) - labelbrightness = 100 - clab.l - sethue(convert(RGB, Lab(labelbrightness, clab.b, clab.a))) - text(string(cols[n][1]), pos, halign=:center) +fontface("AvenirNextCondensed-Regular") # hide +fontsize(8) +cols = sort(collect(Colors.color_names)) +ncols = 15 +nrows = convert(Int, ceil(length(cols) / ncols)) +table = Table(nrows, ncols, 800/ncols, 800/nrows) +gamma = 2.2 +for n in 1:length(cols) + col = cols[n][1] + r, g, b = sethue(col) + box(table[n], table.colwidths[1], table.rowheights[1], :fill) + luminance = 0.2126 * r^gamma + 0.7152 * g^gamma + 0.0722 * b^gamma + (luminance > 0.5^gamma) ? sethue("black") : sethue("white") + text(string(cols[n][1]), table[n], halign=:center, valign=:middle) end finish() # hide -nothing # hide + +nothing #hide ``` -![line endings](assets/figures/colors.png) +![line endings](assets/figures/colors.svg) -Some fiddling with Lab colors adjusts the label color to make it stand out against the background. +(To make the label stand out against the background, the luminance is calculated, then used to choose the label's color.) ```@docs sethue diff --git a/docs/src/polygons.md b/docs/src/polygons.md index 602af50a..a3077b05 100644 --- a/docs/src/polygons.md +++ b/docs/src/polygons.md @@ -56,7 +56,7 @@ t = Table(fill(20, nrows), widths) for r in 1:size(t)[1] for c in 1:size(t)[2] @layer begin - sethue("thistle") + sethue("thistle1") if r >= 2 && c >= 2 if isodd(c) setopacity(0.5) diff --git a/docs/src/tables-grids.md b/docs/src/tables-grids.md index 1ac87fbe..4260ce76 100644 --- a/docs/src/tables-grids.md +++ b/docs/src/tables-grids.md @@ -24,12 +24,12 @@ In this example, every third tile is divided up into subtiles and colored: ```@example using Luxor, Random # hide -Drawing(400, 300, "assets/figures/tiler.png") # hide +Drawing(800, 500, "assets/figures/tiler.png") # hide background("white") # hide origin() # hide Random.seed!(1) # hide fontsize(20) # hide -tiles = Tiler(400, 300, 4, 5, margin=5) +tiles = Tiler(800, 500, 4, 5, margin=5) for (pos, n) in tiles randomhue() box(pos, tiles.tilewidth, tiles.tileheight, :fill) @@ -37,6 +37,8 @@ for (pos, n) in tiles gsave() translate(pos) subtiles = Tiler(tiles.tilewidth, tiles.tileheight, 4, 4, margin=5) + + @show tiles[n] for (pos1, n1) in subtiles randomhue() box(pos1, subtiles.tilewidth, subtiles.tileheight, :fill) @@ -60,6 +62,18 @@ Tiler Partition ``` +You can obtain the centerpoints of all the tiles in one go with: + +``` +first.(collect(tiles)) +``` + +or obtain ranges with: + +``` +tiles[1:2:end] +``` + ## Tables The `Table` iterator can be used to define tables: rectangular grids with a specific number of rows and columns. The columns can have different widths, and the rows can have different heights. Tables don't store data, but are designed to help you draw tabular data. diff --git a/src/Table.jl b/src/Table.jl index b0a46dde..8d5269d6 100644 --- a/src/Table.jl +++ b/src/Table.jl @@ -87,7 +87,7 @@ or without iteration, using cellnumber: ``` @svg begin t = Table(8, 3, 30, 15) - for n in 1:length(t) + for n in eachindex(t) randomhue() box(t, n, :fill) sethue("white") diff --git a/src/drawings.jl b/src/drawings.jl index 2d01045a..884dcb94 100644 --- a/src/drawings.jl +++ b/src/drawings.jl @@ -275,23 +275,20 @@ function preview() display_ijulia(MIME("image/png"), current_filename()) elseif current_surface_type() == :svg display_ijulia(MIME("image/svg+xml"), current_filename()) - # open(current_filename()) do f - # display("image/svg+xml", read(f, String)) - # end end elseif candisplay && juno display(CURRENTDRAWING[1]) returnvalue = nothing elseif Sys.isapple() - run(`open $(current_filename())`) returnvalue = current_filename() + run(`open $(returnvalue)`) elseif Sys.iswindows() - cmd = get(ENV, "COMSPEC", "cmd") - run(`$(ENV["COMSPEC"]) /c start $(filename)`) returnvalue = current_filename() + cmd = get(ENV, "COMSPEC", "cmd") + run(`$(ENV["COMSPEC"]) /c start $(returnvalue)`) elseif Sys.isunix() - run(`xdg-open $(current_filename())`) returnvalue = current_filename() + run(`xdg-open $(returnvalue)`) end return returnvalue end diff --git a/src/polygons.jl b/src/polygons.jl index c447c4d0..0b61711a 100644 --- a/src/polygons.jl +++ b/src/polygons.jl @@ -183,7 +183,7 @@ inadequacy. By default these will generate errors, but you can suppress these by function isinside(p::Point, pointlist::AbstractArray{Point, 1}; allowonedge::Bool=false) c = false - @inbounds for counter in 1:length(pointlist) + @inbounds for counter in eachindex(pointlist) q1 = pointlist[counter] # if reached last point, set "next point" to first point if counter == length(pointlist) @@ -758,7 +758,7 @@ Return an array of the points where a line between pt1 and pt2 crosses polygon C """ function intersectlinepoly(pt1::Point, pt2::Point, C::AbstractArray{Point, 1}) intersectingpoints = Point[] - for j in 1:length(C) + for j in eachindex(C) Cpointpair = (C[j], C[mod1(j+1, length(C))]) flag, pt = intersection(pt1, pt2, Cpointpair..., crossingonly=true) if flag @@ -779,7 +779,7 @@ polygon C. Calls `intersectlinepoly()`. """ function polyintersections(S::AbstractArray{Point, 1}, C::AbstractArray{Point, 1}) Splusintersectionpoints = Point[] - for i in 1:length(S) + for i in eachindex(S) Spointpair = (S[i], S[mod1(i+1, length(S))]) push!(Splusintersectionpoints, S[i]) for pt in intersectlinepoly(Spointpair..., C) diff --git a/src/text.jl b/src/text.jl index 1ada924e..f65a95e0 100644 --- a/src/text.jl +++ b/src/text.jl @@ -491,7 +491,7 @@ function textlines(s::T where T<:AbstractString, width::Real; rightgutter=5) push!(result, strip(join(currentline))) # strip trailing spaces - for i in 1:length(result) + for i in eachindex(result) result[i] = strip(result[i]) end return result diff --git a/src/tiles-grids.jl b/src/tiles-grids.jl index ed75ad30..3e003c66 100644 --- a/src/tiles-grids.jl +++ b/src/tiles-grids.jl @@ -106,6 +106,23 @@ function Base.getindex(pt::Tiler, i::Int) return (Point(xcoord, ycoord), i) end +function Base.size(pt::Tiler) + return (pt.nrows, pt.ncols) +end + +function Base.length(pt::Tiler) + pt.nrows * pt.ncols +end + +Base.lastindex(pt::Tiler) = length(pt) +Base.firstindex(pt::Tiler) = 1 + +Base.eltype(::Type{Tiler}) = Tuple + +Base.getindex(pt::Tiler, I) = [pt[i] for i in I] + +Base.IteratorSize(pt::Tiler) = Base.HasShape{2}() + """ GridRect(startpoint, xspacing, yspacing, width, height) diff --git a/test/pagetiler-test.jl b/test/pagetiler-test.jl index 90e51869..938c7299 100644 --- a/test/pagetiler-test.jl +++ b/test/pagetiler-test.jl @@ -7,12 +7,11 @@ using Test using Random Random.seed!(42) -setantialias(6) - fname = "tiler-test1.pdf" pagewidth, pageheight = 600, 900 Drawing(pagewidth, pageheight, fname) origin() # move 0/0 to center +setantialias(6) background("ivory") setopacity(0.9) setline(0.6) @@ -28,9 +27,8 @@ for (pos, n) in pagetiles ellipse(pos, pagetiles.tilewidth, pagetiles.tileheight, :fill) end -# testing eachindex -for i in 1:length(pagetiles) - cpos, n = pagetiles[i] +for tile in pagetiles + cpos, n = tile for j in 1:20 box(cpos, 5j, 5j, :stroke) end diff --git a/test/table-tests.jl b/test/table-tests.jl index c3d80d7b..264e0596 100644 --- a/test/table-tests.jl +++ b/test/table-tests.jl @@ -35,7 +35,7 @@ function testtable(fname) sethue("black") fontsize(10) - for n in 1:length(t) + for (pt, n) in t label(string(n), :n, t[n], offset=15) end