Skip to content

Commit

Permalink
Add a background color CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbrooks committed Jan 13, 2025
1 parent 0596303 commit ae19a13
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
11 changes: 3 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions src/main/kotlin/com/jzbrooks/vat/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,7 +42,7 @@ fun main(args: Array<String>) {
return
}

val scale = argReader.readOption("s|scale")?.let {
val scale = argReader.readOption("scale|s")?.let {
val factor = it.toFloatOrNull()
if (factor != null) {
factor
Expand All @@ -51,6 +52,17 @@ fun main(args: Array<String>) {
}
} ?: 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 ->
Expand Down Expand Up @@ -88,7 +100,7 @@ fun main(args: Array<String>) {
}

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)
Expand Down

0 comments on commit ae19a13

Please sign in to comment.