Skip to content

Commit 7e3c8ac

Browse files
authored
Merge pull request #37 from matt-dray/36-pixeltrix-class
Add pixeltrix class
2 parents 0bc3934 + ca5a4be commit 7e3c8ac

23 files changed

+471
-353
lines changed

DESCRIPTION

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
Package: pixeltrix
2-
Title: Simple Interactive Pixel Art
3-
Version: 0.2.1.9000
2+
Title: A Simple Interactive Pixel Art Editor
3+
Version: 0.2.2
44
Authors@R:
55
person("Matt", "Dray", , "[email protected]", role = c("aut", "cre"))
66
Description: A very simple 'pixel art' tool that lets you click squares
7-
interactively in a plot window and returns your final image as a matrix.
8-
Provide multiple frames to create an animated gif.
7+
interactively in a plot window and returns your final image as a
8+
matrix. Provide multiple frames to create an animated gif.
99
License: MIT + file LICENSE
10-
URL: https://github.com/matt-dray/pixeltrix, https://matt-dray.github.io/pixeltrix
10+
URL: https://github.com/matt-dray/pixeltrix,
11+
https://matt-dray.github.io/pixeltrix
1112
BugReports: https://github.com/matt-dray/pixeltrix/issues
12-
Encoding: UTF-8
13-
Roxygen: list(markdown = TRUE)
14-
RoxygenNote: 7.1.2
13+
Depends:
14+
R (>= 2.10)
1515
Suggests:
1616
gifski,
1717
testthat (>= 3.0.0)
1818
Config/testthat/edition: 3
19-
Depends:
20-
R (>= 2.10)
19+
Encoding: UTF-8
2120
LazyData: true
21+
Roxygen: list(markdown = TRUE)
22+
RoxygenNote: 7.3.1

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(as_pixeltrix)
34
export(click_pixels)
45
export(draw_pixels)
56
export(edit_pixels)
67
export(frame_pixels)
78
export(gif_pixels)
9+
export(is_pixeltrix)

NEWS.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# pixeltrix 0.2.1.9000
1+
# pixeltrix 0.2.2
22

3-
* Added warning to README and function docs about flickering and the bell (#31, #32, thanks @adrianadepalma)
3+
* Added 'pixeltrix' class to matrix objects output by `click_pixels()` and `edit_pixels()`, and included `is_pixeltrix()` and `as_pixeltrix()` to detect and coerce (#36, thanks @trevorld).
4+
* Added warning to README and function docs about flickering and the bell (#31, #32, thanks @adrianadepalma).
45

56
# pixeltrix 0.2.1
67

7-
* Added additional input checks to check-utils.R.
8+
* Added additional input checks to `check-utils.R`.
89
* `draw_pixels()` now finds `n_states` (#24).
910
* Allow for an increased number of states in `edit_pixels()` (#25).
1011

@@ -14,7 +15,7 @@
1415
* A named character vector of colours is now accepted as input to `click_pixels()`, `edit pixels()`, `draw_pixels()` and `frame_pixels()`.
1516
* Breaking: `blue` and `mario` demo datasets no longer available; `pkmn_sprite` and `mario_frames` replace them (#22).
1617
* Updated and expanded function documentation and README (#21).
17-
* Reused input checks have been generalised into 'R/utils-check.R'.
18+
* Reused input checks have been generalised into `R/utils-check.R`.
1819
* Expanded tests to cover argument input errors.
1920

2021
# pixeltrix 0.1.3
@@ -45,7 +46,7 @@
4546
# pixeltrix 0.0.0.9004
4647

4748
* Added an `edit_pixels()` function so that a matrix created with `click_pixels()` can be updated (#6).
48-
* Moved support functions to utils.R.
49+
* Moved support functions to `utils.R`.
4950
* Simplified the README and added a blog badge.
5051

5152
# pixeltrix 0.0.0.9003
@@ -58,7 +59,7 @@
5859
# pixeltrix 0.0.0.9002
5960

6061
* Went the non-Shiny route and used `locator()` and `image()` for interactivity in the `click_pixel()` function (#2).
61-
* Added GitHub Actions for R-CMD check, tests, {pkgdown} webbite.
62+
* Added GitHub Actions for R-CMD check, tests, {pkgdown} website.
6263
* This version was [previewed in a tweet](https://twitter.com/mattdray/status/1573053714788753408?s=20&t=0HzMLD0fjc5evjtCKqQY3g).
6364

6465
# pixeltrix 0.0.0.9001

R/animate.R

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@
4040
#' @export
4141
#'
4242
#' @examples \dontrun{
43-
#' # Begin interactive sequence to create animation frames
44-
#' my_frames <- frame_pixels(
45-
#' n_rows = 16L,
46-
#' n_cols = 16L,
47-
#' n_states = 3L,
48-
#' colours = c("grey25", "green", "#0000FF")
49-
#' )
50-
#' }
43+
#' # Begin interactive sequence to create animation frames
44+
#' my_frames <- frame_pixels(
45+
#' n_rows = 16L,
46+
#' n_cols = 16L,
47+
#' n_states = 3L,
48+
#' colours = c("grey25", "green", "#0000FF")
49+
#' )}
5150
frame_pixels <- function(
5251
n_rows = 8L,
5352
n_cols = 8L,
@@ -115,21 +114,20 @@ frame_pixels <- function(
115114
#' @export
116115
#'
117116
#' @examples \dontrun{
118-
#' # Begin interactive sequence to create animation frames
119-
#' my_frames <- frame_pixels(
120-
#' n_rows = 16L,
121-
#' n_cols = 16L,
122-
#' n_states = 3L,
123-
#' colours = c("grey25", "green", "#0000FF")
124-
#' )
117+
#' # Begin interactive sequence to create animation frames
118+
#' my_frames <- frame_pixels(
119+
#' n_rows = 16L,
120+
#' n_cols = 16L,
121+
#' n_states = 3L,
122+
#' colours = c("grey25", "green", "#0000FF")
123+
#' )
125124
#'
126-
#' # Write list of matrices to gif (requires 'gifski' installation)
127-
#' gif_pixels(
128-
#' frames = my_frames,
129-
#' file = "example.gif", # location to write GIF
130-
#' delay = 0.1 # passed to gifski::save_gif()
131-
#' )
132-
#' }
125+
#' # Write list of matrices to gif (requires 'gifski' installation)
126+
#' gif_pixels(
127+
#' frames = my_frames,
128+
#' file = "example.gif", # location to write GIF
129+
#' delay = 0.1 # passed to gifski::save_gif()
130+
#' )}
133131
gif_pixels <- function(
134132
frames,
135133
colours = NULL,

R/click.R

Lines changed: 89 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@
2929
#' bell sound on click, which you can disable by setting
3030
#' `options(locatorBell = FALSE)`.
3131
#'
32-
#' @return A matrix. The zero-indexed values correspond to the state of each
33-
#' pixel, which is determined by the number of clicks. Has an additional
34-
#' attribute: a named character vector, 'colours', which maps the values in
35-
#' the matrix to the colours provided by the user (or a gradated set of
36-
#' greys provided by default when the 'colours' argument is NULL).
32+
#' @return A 'pixeltrix'-class matrix. The zero-indexed values correspond to the
33+
#' state of each pixel, which is determined by the number of clicks a user
34+
#' gave each pixel. Has a named character-vector attribute, 'colours', which
35+
#' maps the matrix values to hex colours provided by the user (or a gradated
36+
#' set of greys provided by default when the 'colours' argument is NULL).
3737
#'
3838
#' @export
3939
#'
4040
#' @examples \dontrun{
41-
#' # Create a 16 x 16 pixel matrix with 3 possible pixel states
42-
#' my_matrix <- click_pixels(
43-
#' n_rows = 16L,
44-
#' n_cols = 16L,
45-
#' n_states = 3L,
46-
#' colours = c("blue", "#FF0000", "yellow")
47-
#' )
41+
#' # Create a 16 x 16 pixel matrix with 3 possible pixel states
42+
#' my_matrix <- click_pixels(
43+
#' n_rows = 16L,
44+
#' n_cols = 16L,
45+
#' n_states = 3L,
46+
#' colours = c("blue", "#FF0000", "yellow")
47+
#' )
4848
#' }
4949
click_pixels <- function(
5050
n_rows = 8L,
@@ -79,7 +79,8 @@ click_pixels <- function(
7979
if (grid) .add_grid(m)
8080
m <- .repeat_loop(m, n_states, colours, grid)
8181

82-
# Add colours as an attribute to returned matrix
82+
# Add class and colours as attributes to returned matrix
83+
class(m) <- "pixeltrix"
8384
attr(m, "colours") <- stats::setNames(colours, seq(0, n_states - 1))
8485

8586
m
@@ -121,29 +122,32 @@ click_pixels <- function(
121122
#' bell sound on click, which you can disable by setting
122123
#' `options(locatorBell = FALSE)`.
123124
#'
124-
#' @return A matrix.
125+
#' @return A 'pixeltrix'-class matrix. The zero-indexed values correspond to the
126+
#' state of each pixel, which is determined by the number of clicks a user
127+
#' gave each pixel. Has a named character-vector attribute, 'colours', which
128+
#' maps the matrix values to hex colours provided by the user (or a gradated
129+
#' set of greys provided by default when the 'colours' argument is NULL).
125130
#'
126131
#' @export
127132
#'
128133
#' @examples \dontrun{
129-
#' # Create a 3 x 4 pixel matrix with 3 possible states to cycle through
130-
#' my_matrix <- click_pixels(
131-
#' n_rows = 3L,
132-
#' n_cols = 4L,
133-
#' n_states = 3L,
134-
#' colours = c("white", "red", "#0000FF")
135-
#' )
136-
#'
137-
#'. # Update the original matrix
138-
#' my_matrix_edited <- edit_pixels(m = my_matrix)
139-
#'
140-
#' # Update the original matrix with additional state, different colours
141-
#' my_matrix_augmented <- edit_pixels(
142-
#' m = my_matrix,
143-
#' n_states = 4L, # one more than in the original
144-
#' colours = c("bisque3", "orchid", "chartreuse", "olivedrab")
145-
#' )
146-
#' }
134+
#' # Create a 3 x 4 pixel matrix with 3 possible states to cycle through
135+
#' my_matrix <- click_pixels(
136+
#' n_rows = 3L,
137+
#' n_cols = 4L,
138+
#' n_states = 3L,
139+
#' colours = c("white", "red", "#0000FF")
140+
#' )
141+
#'
142+
#'.# Update the original matrix
143+
#' my_matrix_edited <- edit_pixels(m = my_matrix)
144+
#'
145+
#' # Update the original matrix with additional state, different colours
146+
#' my_matrix_augmented <- edit_pixels(
147+
#' m = my_matrix,
148+
#' n_states = 4L, # one more than in the original
149+
#' colours = c("bisque3", "orchid", "chartreuse", "olivedrab")
150+
#' )}
147151
edit_pixels <- function(
148152
m,
149153
n_states = NULL,
@@ -182,9 +186,61 @@ edit_pixels <- function(
182186
if (grid) .add_grid(m)
183187
m <- .repeat_loop(m, n_states, colours, grid)
184188

185-
# Add colours as an attribute to returned matrix
189+
# Add class and colours as attributes to returned matrix
190+
class(m) <- "pixeltrix"
191+
attr(m, "colours") <- stats::setNames(colours, seq(0, n_states - 1))
192+
193+
m
194+
195+
}
196+
197+
#' Coerce to a 'pixeltrix' Object
198+
#'
199+
#' Functions to check if an object is of 'pixeltrix' class, or coerce to it if
200+
#' possible.
201+
#'
202+
#' @param m A matrix of integers to coerce.
203+
#'
204+
#' @details To be successfully coerced, `m` must be a matrix composed only of
205+
#' integers.
206+
#'
207+
#' @return \code{as_pixeltrix} returns an object of class 'pixeltrix' if
208+
#' possible. \code{is_pixeltrix} returns \code{TRUE} if the object has class
209+
#' 'pixeltrix', otherwise \code{FALSE}. If coerced, a named character-vector
210+
#' attribute, 'colours', is also added, which maps the matrix values to a
211+
#' gradated palette of greys in hex form.
212+
#'
213+
#' @examples
214+
#' m <- matrix(c(0L, 1L, 1L, 0L), 2, 2)
215+
#' is_pixeltrix(m)
216+
#' m
217+
#'
218+
#' m <- as_pixeltrix(m)
219+
#' is_pixeltrix(m)
220+
#' m
221+
#'
222+
#' @export
223+
as_pixeltrix <- function(m) {
224+
225+
.check_matrix(m)
226+
227+
if (inherits(m, "pixeltrix")) {
228+
return(m)
229+
}
230+
231+
n_states <- max(unique(as.vector(m)) + 1L)
232+
get_greys <- grDevices::colorRampPalette(c("white", "grey20"))
233+
colours <- get_greys(n_states)
234+
235+
class(m) <- "pixeltrix"
186236
attr(m, "colours") <- stats::setNames(colours, seq(0, n_states - 1))
187237

188238
m
189239

190240
}
241+
242+
#' @rdname as_pixeltrix
243+
#' @export
244+
is_pixeltrix <- function(m) {
245+
inherits(m, "pixeltrix")
246+
}

R/data.R

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
#' in a font with letters that are 3 by 3 pixels.
66
#'
77
#' @examples \dontrun{
8-
#' # Plot the matrix as an image
9-
#' draw_pixels(m = pixeltrix::logo)
10-
#' }
8+
#' # Plot the matrix as an image
9+
#' draw_pixels(m = pixeltrix::logo)}
1110
#'
12-
#' @format A matrix with 10 rows and 21 columns, taking integers from 0 to 2.
11+
#' @format A 'pixeltrix'-class matrix with 10 rows and 21 columns, taking
12+
#' integers from 0 to 2.
1313
"logo"
1414

1515
#' Example Sprite Matrix: Mario
@@ -19,16 +19,15 @@
1919
#' the walk cycle.
2020
#'
2121
#' @examples \dontrun{
22-
#' # Write the list of matrices to a gif
23-
#' gif_pixels(
24-
#' frames = pixeltrix::mario_frames,
25-
#' file = "mario.gif",
26-
#' delay = 0.15 # passed to gifski::save_gif()
27-
#' )
28-
#' }
22+
#' # Write the list of matrices to a gif
23+
#' gif_pixels(
24+
#' frames = pixeltrix::mario_frames,
25+
#' file = "mario.gif",
26+
#' delay = 0.15 # passed to gifski::save_gif()
27+
#' )}
2928
#'
30-
#' @format A list of matrices, each with 16 rows and 16 columns, taking integers
31-
#' from 0 to 2.
29+
#' @format A list of 'pixeltrix'-class matrices, each with 16 rows and 16
30+
#' columns, taking integers from 0 to 2.
3231
#'
3332
#' @source Super Mario Bros (1983), Nintendo.
3433
"mario_frames"
@@ -39,14 +38,14 @@
3938
#' a player sprite from Pokémon.
4039
#'
4140
#' @examples \dontrun{
42-
#' # Plot the matrix as an image
43-
#' draw_pixels(
44-
#' m = pixeltrix::pkmn_sprite,
45-
#' colours = c("#9bbc0f", "#8bac0f", "#306230")
46-
#' )
47-
#' }
48-
#'
49-
#' @format A matrix with 16 rows and 14 columns, taking integers from 0 to 2.
41+
#' # Plot the matrix as an image
42+
#' draw_pixels(
43+
#' m = pixeltrix::pkmn_sprite,
44+
#' colours = c("#9bbc0f", "#8bac0f", "#306230")
45+
#' )}
46+
#'
47+
#' @format A 'pixeltrix'-class matrix with 16 rows and 14 columns, taking
48+
#' integers from 0 to 2.
5049
#'
5150
#' @source Hand-copied from Pokemon (1996), The Pokemon Company.
5251
"pkmn_sprite"

R/draw.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
#' @export
1919
#'
2020
#' @examples \dontrun{
21-
#' my_matrix <- click_pixels(n_states = 3L)
22-
#' draw_pixels(my_matrix, c("black", "#0000FF", "green")) # one colour per state
23-
#' }
21+
#' my_matrix <- click_pixels(n_states = 3L)
22+
#' draw_pixels(my_matrix, c("black", "#0000FF", "green")) # a colour per state}
2423
draw_pixels <- function(m, colours = NULL) {
2524

2625
.check_matrix(m)

0 commit comments

Comments
 (0)