#svg #color #invert #command-line-tool #library

bin+lib svg-invert

A CLI utility and a library to invert colors in an SVG image

2 releases

0.0.2 May 12, 2024
0.0.1 May 11, 2024

#1086 in Command line utilities

25 downloads per month

MIT license

60KB
234 lines

svg-invert

Build Status codecov Crates.io Documentation

A CLI utility that inverts colors in an SVG file.


💁‍♂️ Use case

If you ever need to invert all the colors (fill and stroke) in an SVG file, this simple CLI utility is for you.

My original use case was to be able to invert black and white-ish diagrams so that they can be used in both light and dark mode in an e-book, but I reckon this utility can fulfill many other use cases. Let me know what you'll use it for! 🚀

🛠️ Installation

You can install svg-invert using precompiled binaries (if available for your operative system and architecture) or by compiling it from source.

Using precompiled binaries

You can download precompiled binaries from the releases page and place them in a directory that is in your PATH.

If you have cargo binstall in your system, you can use it to install the latest version of svg-invert:

cargo binstall svg-invert

This method has the advantage of automatically placing the binary in a directory that is in your PATH. Also, if a binary is not available for your operative system and architecture, cargo binstall will compile it for you (assuming you have all the necessary Rust build toolchain in your system).

Compiling from source

If you have the Rust toolchain installed in your system, you can compile svg-invert from source using cargo:

cargo install svg-invert

👩‍🏫 Usage

Right now, svg-invert offers only a very simple and minimal interface: data in from stdin and data out to stdout:

svg-invert < some-lovely.svg > inverted-some-lovely.svg

or, if you like Unix pipes:

cat some-lovely.svg | svg-invert > inverted-some-lovely.svg

For example, if this is the content of some-lovely.svg:

A lovely crab with a Lambda hat

After running the command, this will be the content of inverted-some-lovely.svg:

A lovely crab with a Lambda hat with inverted colours

[!NOTE] If think this little crab is cute, you should check out my book Crafting Lambda Functions in Rust where you can learn how to build serverless applications with Rust and AWS Lambda! 🦀🚀 Fun fact: This utility was actually born as a way to create SVGs diagrams that look good on both the light and dark version of the e-book!

Usage as a library

You can use this utility also as part of you own Rust program by using it as a library.

You can install the library with:

cargo add svg-invert

Then you can use it in your Rust program like this:

use std::io::{stdin, stdout, BufReader, BufWriter, Write};
use svg_invert::invert_svg;

let reader = BufReader::new(stdin());
let writer = BufWriter::new(stdout());
match invert_svg(reader, writer) {
  Ok(_) => {
    // makes sure to flush stdout before exiting
    // since we are using a BufWriter
    match stdout().flush() {
      Ok(_) => {}
      Err(e) => {
        eprintln!("Error: {e}");
      }
    }
  }
  Err(e) => {
    eprintln!("Error: {e}");
  }
}

Processing multiple images

svg_invert::invert_svg is a shortcut for creating a new InvertSvg instance and calling its invert_svg method.

If you intend to process multiple SVG images in the same program, it might be beneficial to create a single InvertSvg instance and reuse it.

In fact, an InvertSvg instance holds an internal cache that stores the inverted colors of the SVG images it processes. So, by reusing the same instance, you might have some performance gains if your images happen to share the same colors.

use svg_invert::InvertSvg;

let invert_svg = InvertSvg::new();
// call invert_svg.invert_svg(...) multiple times

👷 Contributing

Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.

👩‍⚖️ License

Licensed under MIT License. © Luciano Mammino.

Dependencies

~0.6–1MB
~23K SLoC