Skip to content

Commit ba25073

Browse files
committed
refactor: refactor 'fill_with'
1 parent 4cc7291 commit ba25073

File tree

5 files changed

+40
-48
lines changed

5 files changed

+40
-48
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: aznyan
33
Title: Image Filters with 'OpenCV'
4-
Version: 26.02.05
4+
Version: 26.02.09
55
Authors@R: c(
66
person("Akiru", "Kato", , "paithiov909@gmail.com", role = c("aut", "cre")),
77
person("Jeroen", "Ooms", role = "cph")
@@ -16,6 +16,7 @@ Depends:
1616
R (>= 4.2)
1717
Imports:
1818
cli,
19+
colorfast,
1920
grDevices,
2021
grid,
2122
rlang

R/aznyan-package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ check_nr_dim <- function(src, dst) {
7777
#' @param x Object to be set class.
7878
#' @noRd
7979
as_nr <- function(x) {
80-
class(x) <- c("nativeRaster", class(x))
80+
class(x) <- "nativeRaster"
8181
x
8282
}
8383

R/color-filters.R

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ apply_1977 <- function(nr) {
108108
contrast(nr, .1) |>
109109
brighten(.1) |>
110110
saturate(.3)
111-
fg <- fill_with(ncol(nr), nrow(nr), premul(243, 106, 188, 76))
111+
fg <- fill_with(premul(243, 106, 188, 76), ncol(nr), nrow(nr))
112112
blend_screen(bg, fg) # bg, fg
113113
}
114114

@@ -125,7 +125,7 @@ apply_aden <- function(nr) {
125125
apply_brannan <- function(nr) {
126126
bg <- sepia(nr, .2) |>
127127
contrast(.2)
128-
fg <- fill_with(ncol(nr), nrow(nr), premul(161, 44, 199, 59))
128+
fg <- fill_with(premul(161, 44, 199, 59), ncol(nr), nrow(nr))
129129
blend_lighten(fg, bg)
130130
}
131131

@@ -134,23 +134,23 @@ apply_brooklyn <- function(nr) {
134134
bg <- contrast(nr, -.1) |>
135135
brighten(.1) |>
136136
reset_alpha()
137-
fg <- fill_with(ncol(nr), nrow(nr), premul(168, 223, 193, 150))
137+
fg <- fill_with(premul(168, 223, 193, 150), ncol(nr), nrow(nr))
138138
blend_overlay(fg, bg)
139139
}
140140

141141
#' @noRd
142142
apply_clarendon <- function(nr) {
143143
bg <- contrast(nr, .2) |>
144144
saturate(.35)
145-
fg <- fill_with(ncol(nr), nrow(nr), premul(127, 187, 227, 101))
145+
fg <- fill_with(premul(127, 187, 227, 101), ncol(nr), nrow(nr))
146146
blend_overlay(fg, bg)
147147
}
148148

149149
#' @noRd
150150
apply_earlybird <- function(nr) {
151151
bg <- contrast(nr, -.1) |>
152152
sepia(.05)
153-
fg <- fill_with(ncol(nr), nrow(nr), premul(208, 186, 142, 150))
153+
fg <- fill_with(premul(208, 186, 142, 150), ncol(nr), nrow(nr))
154154
out <- blend_overlay(bg, fg) # bg, fg
155155
reset_alpha(out)
156156
}
@@ -159,7 +159,7 @@ apply_earlybird <- function(nr) {
159159
apply_gingham <- function(nr) {
160160
bg <- brighten(nr, .05) |>
161161
hue_rotate(-.1745329) # -10 deg
162-
fg <- fill_with(ncol(nr), nrow(nr), premul(230, 230, 230, 255))
162+
fg <- fill_with(premul(230, 230, 230, 255), ncol(nr), nrow(nr))
163163
blend_softlight(fg, bg)
164164
}
165165

@@ -168,7 +168,7 @@ apply_hudson <- function(nr) {
168168
bg <- brighten(nr, .5) |>
169169
contrast(-.1) |>
170170
saturate(.1)
171-
fg <- fill_with(ncol(nr), nrow(nr), premul(166, 177, 255, 208))
171+
fg <- fill_with(premul(166, 177, 255, 208), ncol(nr), nrow(nr))
172172
out <- blend_multiply(fg, bg)
173173
reset_alpha(out)
174174
}
@@ -186,19 +186,19 @@ apply_kelvin <- function(nr) {
186186
bg <-
187187
blend_colordodge(
188188
nr,
189-
fill_with(ncol(nr), nrow(nr), premul(56, 44, 52, 255))
189+
fill_with(premul(56, 44, 52, 255), ncol(nr), nrow(nr))
190190
)
191-
fg <- fill_with(ncol(nr), nrow(nr), premul(183, 125, 33, 255))
191+
fg <- fill_with(premul(183, 125, 33, 255), ncol(nr), nrow(nr))
192192
blend_overlay(fg, bg)
193193
}
194194

195195
#' @noRd
196196
apply_lark <- function(nr) {
197197
bg <- contrast(nr, -.1) |>
198198
blend_colordodge(
199-
fill_with(ncol(nr), nrow(nr), premul(34, 37, 63, 255))
199+
fill_with(premul(34, 37, 63, 255), ncol(nr), nrow(nr))
200200
)
201-
fg <- fill_with(ncol(nr), nrow(nr), premul(242, 242, 242, 204))
201+
fg <- fill_with(premul(242, 242, 242, 204), ncol(nr), nrow(nr))
202202
blend_darken(fg, bg)
203203
}
204204

@@ -222,7 +222,7 @@ apply_maven <- function(nr) {
222222
apply_mayfair <- function(nr) {
223223
bg <- contrast(nr, .1) |>
224224
saturate(.1)
225-
fg <- fill_with(ncol(nr), nrow(nr), premul(255, 200, 200, 153))
225+
fg <- fill_with(premul(255, 200, 200, 153), ncol(nr), nrow(nr))
226226
blend_overlay(fg, bg)
227227
}
228228

@@ -231,10 +231,10 @@ apply_moon <- function(nr) {
231231
bg <- contrast(nr, .1) |>
232232
brighten(.1) |>
233233
blend_softlight(
234-
fill_with(ncol(nr), nrow(nr), premul(160, 160, 160, 255)),
234+
fill_with(premul(160, 160, 160, 255), ncol(nr), nrow(nr)),
235235
dst = _
236236
)
237-
fg <- fill_with(ncol(nr), nrow(nr), premul(56, 56, 56, 255))
237+
fg <- fill_with(premul(56, 56, 56, 255), ncol(nr), nrow(nr))
238238
out <- blend_lighten(fg, bg)
239239
grayscale(out)
240240
}
@@ -246,10 +246,10 @@ apply_nashville <- function(nr) {
246246
brighten(.05) |>
247247
saturate(.2) |>
248248
blend_darken(
249-
fill_with(ncol(nr), nrow(nr), premul(247, 176, 153, 243)),
249+
fill_with(premul(247, 176, 153, 243), ncol(nr), nrow(nr)),
250250
dst = _
251251
)
252-
fg <- fill_with(ncol(nr), nrow(nr), premul(0, 70, 150, 230))
252+
fg <- fill_with(premul(0, 70, 150, 230), ncol(nr), nrow(nr))
253253
blend_lighten(fg, bg)
254254
}
255255

@@ -259,7 +259,7 @@ apply_reyes <- function(nr) {
259259
brighten(.1) |>
260260
contrast(-.15) |>
261261
saturate(-.25)
262-
fg <- fill_with(ncol(nr), nrow(nr), premul(239, 205, 173, 10))
262+
fg <- fill_with(premul(239, 205, 173, 10), ncol(nr), nrow(nr))
263263
blend_over(fg, bg)
264264
}
265265

@@ -269,9 +269,9 @@ apply_rise <- function(nr) {
269269
sepia(.05) |>
270270
contrast(-.1) |>
271271
saturate(-.1)
272-
fg <- fill_with(ncol(nr), nrow(nr), premul(236, 205, 169, 240))
272+
fg <- fill_with(premul(236, 205, 169, 240), ncol(nr), nrow(nr))
273273
bg <- blend_multiply(fg, bg)
274-
fg <- fill_with(ncol(nr), nrow(nr), premul(232, 197, 152, 10))
274+
fg <- fill_with(premul(232, 197, 152, 10), ncol(nr), nrow(nr))
275275
fg <- blend_overlay(fg, bg)
276276
blend_over(fg, nr)
277277
}
@@ -280,9 +280,9 @@ apply_rise <- function(nr) {
280280
apply_slumber <- function(nr) {
281281
bg <- saturate(nr, -.34) |>
282282
brighten(-.05)
283-
fg <- fill_with(ncol(nr), nrow(nr), premul(69, 41, 12, 102))
283+
fg <- fill_with(premul(69, 41, 12, 102), ncol(nr), nrow(nr))
284284
bg <- blend_lighten(fg, bg)
285-
fg <- fill_with(ncol(nr), nrow(nr), premul(125, 105, 24, 128))
285+
fg <- fill_with(premul(125, 105, 24, 128), ncol(nr), nrow(nr))
286286
blend_softlight(fg, bg)
287287
}
288288

@@ -291,15 +291,15 @@ apply_stinson <- function(nr) {
291291
bg <- contrast(nr, -.25) |>
292292
saturate(-.15) |>
293293
brighten(.15)
294-
fg <- fill_with(ncol(nr), nrow(nr), premul(240, 149, 128, 51))
294+
fg <- fill_with(premul(240, 149, 128, 51), ncol(nr), nrow(nr))
295295
blend_softlight(fg, bg)
296296
}
297297

298298
#' @noRd
299299
apply_toaster <- function(nr) {
300300
bg <- contrast(nr, .2) |>
301301
brighten(-.1)
302-
fg <- fill_with(ncol(nr), nrow(nr), premul(128, 78, 15, 140))
302+
fg <- fill_with(premul(128, 78, 15, 140), ncol(nr), nrow(nr))
303303
blend_screen(fg, bg)
304304
}
305305

@@ -308,7 +308,7 @@ apply_valencia <- function(nr) {
308308
bg <- contrast(nr, .08) |>
309309
brighten(.08) |>
310310
sepia(.08)
311-
fg <- fill_with(ncol(nr), nrow(nr), premul(58, 3, 57, 128))
311+
fg <- fill_with(premul(58, 3, 57, 128), ncol(nr), nrow(nr))
312312
blend_exclusion(fg, bg)
313313
}
314314

@@ -318,6 +318,6 @@ apply_walden <- function(nr) {
318318
hue_rotate(-.1745329) |> # -10 deg
319319
saturate(.6) |>
320320
sepia(.05)
321-
fg <- fill_with(ncol(nr), nrow(nr), premul(0, 88, 244, 77))
321+
fg <- fill_with(premul(0, 88, 244, 77), ncol(nr), nrow(nr))
322322
blend_screen(fg, bg)
323323
}

R/color-manip.R

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,13 @@ hls2rgb <- function(x) azny_hls_to_rgb(floor(x))
1616

1717
#' Create a native raster filled with a color
1818
#'
19-
#' @param width,height A positive integer scalar.
2019
#' @param color Color name or hex code.
20+
#' @param width,height A positive integer scalar.
2121
#' @returns A `nativeRaster` object.
2222
#' @export
23-
fill_with <- function(width, height, color) {
24-
packed_int <-
25-
grDevices::col2rgb(color[1], alpha = TRUE) |>
26-
rlang::as_function(
27-
~ {
28-
x <- as.double(.)
29-
azny_pack_integers(x[1:3], x[4], 1, 1)
30-
}
31-
)()
32-
out <- rep_len(packed_int, width * height)
33-
dim(out) <- c(height, width)
23+
fill_with <- function(color, width, height) {
24+
packed_int <- colorfast::col_to_int(color[1])
25+
out <- matrix(packed_int, nrow = height, ncol = width)
3426
as_nr(out)
3527
}
3628

@@ -72,8 +64,8 @@ contrast <- function(nr, intensity) {
7264
#' @export
7365
duotone <- function(nr, color_a = "yellow", color_b = "navy", gamma = 2.2) {
7466
sz <- dim(nr)
75-
color_a <- fill_with(sz[1], sz[2], color_a) |> nr_to_rgba("color_a")
76-
color_b <- fill_with(sz[1], sz[2], color_b) |> nr_to_rgba("color_b")
67+
color_a <- fill_with(color_a, sz[1], sz[2]) |> nr_to_rgba("color_a")
68+
color_b <- fill_with(color_b, sz[1], sz[2]) |> nr_to_rgba("color_b")
7769
ret <- nr_to_rgba(nr, "nr")
7870
luminance <- clamp(gray(ret[1:3, ])^(1 / gamma), 0, 1)
7971
rgb <- mix(color_a[1:3, ], color_b[1:3, ], luminance)
@@ -132,8 +124,8 @@ invert <- function(nr) {
132124
#' @export
133125
linocut <- function(nr, ink = "navy", paper = "snow", threshold = 0.4) {
134126
sz <- dim(nr)
135-
ink <- fill_with(sz[1], sz[2], ink) |> nr_to_rgba("ink")
136-
paper <- fill_with(sz[1], sz[2], paper) |> nr_to_rgba("paper")
127+
ink <- fill_with(ink, sz[1], sz[2]) |> nr_to_rgba("ink")
128+
paper <- fill_with(paper, sz[1], sz[2]) |> nr_to_rgba("paper")
137129
ret <- nr_to_rgba(nr, "nr")
138130
luminance <- step(gray(ret[1:3, ]), threshold)
139131
rgb <- mix(paper[1:3, ], ink[1:3, ], luminance)
@@ -189,8 +181,7 @@ sepia <- function(nr, intensity, depth = 20) {
189181
#' @rdname color-manip
190182
#' @export
191183
set_matte <- function(nr, color = "green") {
192-
rgb_int <-
193-
grDevices::col2rgb(color[1], alpha = FALSE)
184+
rgb_int <- colorfast::col_to_rgb(color[1])
194185
sz <- dim(nr)
195186
ret <- nr_to_rgba(nr, "nr")
196187
ret[1, ][ret[4, ] != 255] <- rgb_int[1, ] * 1

man/fill_with.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)