How to replace colors (shape color and background color)?
Solution 1:
I want to change the white background to #3D94DB (Hex triplet notation) and change the black lines to white.
As you may have already noticed, there are actually subtle gradations (from black to white) of the "black" lines in your example icon (likely due to anti-aliasing). These need to remain intact to help maintain the look of the icon:
Per the Imagemagick usage guide on Level Adjustment by Color, +level-color will help both change the "black" and "white" colors, as well as (roughly) give the proper color gradations:
convert input.png +level-colors white,"#3D94DB" target.png
With your example icon, this command produces the following results:
Caveats:
In the command above, the replacement color order is for black then white.
There should be no spaces between the replacement colors or it will cause the command to fail.
Given the desired effect, the command only works for black and white images. Images with other colors will produce unexpected results. Likewise, two-color images work best if you convert a color image to grayscale.