#!/bin/sh img=$1 color=$2 dither=$3 magick convert "$img" -fuzz 30% -fill black +opaque "$color" -fill white -opaque "$color" /tmp/img_block.png magick convert "$img" -fuzz 1% -fill black +opaque white -fill white -opaque white /tmp/img_white_block.png magick composite /tmp/img_block.png -compose add /tmp/img_white_block.png /tmp/img_full_block.png magick convert "$img" -fuzz 100% -fill "$color" -opaque "$color" /tmp/img_col.png magick composite "$img" -compose difference /tmp/img_col.png /tmp/img_diff.png magick convert /tmp/img_diff.png -set colorspace Gray -separate -average -negate /tmp/img_mask.png magick composite /tmp/img_mask.png -compose multiply /tmp/img_full_block.png /tmp/img_val.png magick convert /tmp/img_val.png -negate -ordered-dither "$dither" -negate /tmp/img_val_dithered.png magick composite /tmp/img_val_dithered.png -compose multiply /tmp/img_col.png img_out_"$color".png