From ae19a1314a224e88e8696d0579d05df8c8068e4a Mon Sep 17 00:00:00 2001 From: Justin Brooks Date: Sun, 12 Jan 2025 20:12:42 -0500 Subject: [PATCH] Add a background color CLI option --- CHANGELOG.md | 11 +++-------- README.md | 9 ++++++--- src/main/kotlin/com/jzbrooks/vat/Main.kt | 22 +++++++++++++++++----- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4050de7..9e9478a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,16 +4,11 @@ ### Added - Build targets for windows and linux systems +- Handle SVGs supported by vgo +- `--background-color` option (hexadecimal RGBA format) ### Changed - -### Deprecated - -### Removed - -### Fixed - -### Security +- Default to transparent background ## 0.0.2 diff --git a/README.md b/README.md index 9c0819c..8e33fd1 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,13 @@ The image can be found at http://plurib.us/1shot/2008/eleven_below/. ``` > vat [options] [file] +vat renders vector artwork (SVG & Android Vector Drawable) to the terminal. + Options: - -s --scale scale factor (float or integer) - -h --help print this message - -v --version print the version number + --background-color background color in hexadecimal RGBA format + -s --scale scale factor (float or integer) + -h --help print this message + -v --version print the version number ``` > `java -jar vat` for Windows diff --git a/src/main/kotlin/com/jzbrooks/vat/Main.kt b/src/main/kotlin/com/jzbrooks/vat/Main.kt index 2006f73..bebebe0 100644 --- a/src/main/kotlin/com/jzbrooks/vat/Main.kt +++ b/src/main/kotlin/com/jzbrooks/vat/Main.kt @@ -19,9 +19,10 @@ private const val HELP_MESSAGE = """ vat renders vector artwork (SVG & Android Vector Drawable) to the terminal. Options: - -s --scale scale factor (float or integer) - -h --help print this message - -v --version print the version number + --background-color background color in hexadecimal RGBA format + -s --scale scale factor (float or integer) + -h --help print this message + -v --version print the version number """ @OptIn(ExperimentalEncodingApi::class) @@ -41,7 +42,7 @@ fun main(args: Array) { return } - val scale = argReader.readOption("s|scale")?.let { + val scale = argReader.readOption("scale|s")?.let { val factor = it.toFloatOrNull() if (factor != null) { factor @@ -51,6 +52,17 @@ fun main(args: Array) { } } ?: 1f + val backgroundColor = argReader.readOption("background-color")?.let { + val rgba = it.removePrefix("0x").chunked(2).mapNotNull { it.toIntOrNull(16) } + if (rgba.size == 4) { + // The RGBA color must be re-packed as ARGB for skia, per its color packing rules + (rgba[3] shl 24) or (rgba[0] shl 16) or (rgba[1] shl 8) or rgba[2] + } else { + System.err.println("Unable to parse $it as a hexadecimal RGBA color e.g. 0xFF0000FF (red)") + null + } + } ?: 0 + val path = argReader.readArguments().first() val image = File(path).inputStream().use { inputStream -> @@ -88,7 +100,7 @@ fun main(args: Array) { } val surface = Surface.makeRasterN32Premul((width * scale).roundToInt(), (height * scale).roundToInt()) - surface.canvas.clear(0) + surface.canvas.clear(backgroundColor) val visitor = DrawingVisitor(surface.canvas, scale, scale) image.accept(visitor)