Skip to content

Commit

Permalink
misc minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cormullion committed Oct 31, 2018
1 parent cdfe5a5 commit b1bc95c
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 39 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions docs/src/assets/examples/sector-chart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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))
Expand Down
36 changes: 21 additions & 15 deletions docs/src/colors-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/src/polygons.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions docs/src/tables-grids.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ 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)
if n % 3 == 0
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)
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 4 additions & 7 deletions src/drawings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/polygons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/text.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/tiles-grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions test/pagetiler-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/table-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b1bc95c

Please sign in to comment.