Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

COLORCONVERTER


Generates a spectrum-like image from the colors in an image

Download Script

last modified: September 26, 2017



USAGE: colorconverter -c "color" [-d dimensions] [-s] [-v]
USAGE: colorconverter [-h or -help]

-c .... color ......... Any valid IM color specification
-d .... dimensions .... Dimensions of optional color swatch; WidthxHeight;
-s .................... Show color swatch of specified color
-v .................... Validate all converted colors by showing color swatches

PURPOSE: To convert any valid ImageMagick color specification to the other ImageMagick color representations.

DESCRIPTION: COLORCONVERTER converts any valid ImageMagick color specification to other ImageMagick color representations, including: RGB, HEX, HSL, HSB and CMYK. The values will be listed to the Terminal. A color swatch for the input color may be displayed. Also a swatch for each of the other color representations may be displayed.

ARGUMENTS:

-c color ... COLOR is any valid IM color specification, including RGB, HEX, HSL, HSB, CMYK or by name. If not a color name, the color specification should be enclosed in quotes.

-d dimensions ... DIMENSIONS specifies the WidthxHeight for the color swatch(es). The default="150x150".

-s ... Indicates to display a color swatch for the specified color.

-v ... Indicates to display a color swatch for each converted color in the various color representations.

NOTE: Prior to IM 6.5.6-6, HSL colors may not produce correct swatches, as changes and bugs were fixed starting with IM 6.5.6-4. Prior to IM 6.5.6-4, HSL colors were specified only with hue in range 0-360 and saturation and lightness as percentages. HSB color specification and swatches were only first available and correct starting with IM 6.5.6-6.

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES (IM 6.5.6-6)


Text Output

Color Swatch

colorconverter -c wheat -s

RGB:
rgb(245,222,179)
rgb(96.0784%,87.0588%,70.1961%)

HEX:
#F5F5DEDEB3B3

HSL:
hsl(39.09,195.696,212)
hsl(10.8583%,76.7437%,83.1373%)

HSB:
hsb(39.09,68.6926,245)
hsb(10.8583%,26.9383%,96.0784%)

CMYK:
cmyk(0,23.9377,68.6926,10)
cmyk(0%,9.38735%,26.9383%,3.92157%)



Text Output

Color Swatch

colorconverter -c "rgb(0,0,255)" -s

RGB:
rgb(0,0,255)
rgb(0%,0%,100%)

HEX:
#00000000FFFF

HSL:
hsl(240,255,127.502)
hsl(66.6667%,100%,50.0008%)

HSB:
hsb(240,255,255)
hsb(66.6667%,100%,100%)

CMYK:
cmyk(255,255,0,0)
cmyk(100%,100%,0%,0%)



Text Output

Color Swatch

colorconverter -c "#FFFF00" -s

RGB:
rgb(255,255,0)
rgb(100%,100%,0%)

HEX:
#FFFFFFFF0000

HSL:
hsl(59.9973,255,127.502)
hsl(16.6659%,100%,50.0008%)

HSB:
hsb(60.0027,255,255)
hsb(16.6674%,100%,100%)

CMYK:
cmyk(0,0,255,0)
cmyk(0%,0%,100%,0%)



Text Output

Color Swatch

colorconverter -c "hsl(120,255,127.5)" -s

RGB:
rgb(0,255,0)
rgb(0%,100%,0%)

HEX:
#0000FFFF0000

HSL:
hsl(120,255,127.502)
hsl(33.3333%,100%,50.0008%)

HSB:
hsb(120,255,255)
hsb(33.3333%,100%,100%)

CMYK:
cmyk(255,0,255,0)
cmyk(100%,0%,100%,0%)



Text Output

Color Swatch

colorconverter -c "hsb(83.3341%,100%,100%)" -s

RGB:
rgb(255,0,254.988)
rgb(100%,0%,99.9954%)

HEX:
#FFFF0000FFFC

HSL:
hsl(300.003,255,127.502)
hsl(83.3341%,100%,50.0008%)
HSB:
hsb(300.003,255,255)
hsb(83.3341%,100%,100%)
CMYK:
cmyk(0,255,0.0116732,0)
cmyk(0%,100%,0.00457771%,0%)



Text Output

Color Swatch

colorconverter -c "cmyk(255,0,0,0)" -s

RGB:
rgb(0,255,255)
rgb(0%,100%,100%)

HEX:
#0000FFFFFFFF

HSL:
hsl(179.997,255,127.502)
hsl(49.9992%,100%,50.0008%)

HSB:
hsb(180.003,255,255)
hsb(50.0008%,100%,100%)

CMYK:
cmyk(255,0,0,0)
cmyk(100%,0%,0%,0%)



What the script does is as follows:

  • Converts the color to each colorspace: RGB, HSL, HSB, CMYK
  • Extracts the raw component values for the IM Q level
  • Converts each component to range 0-255, 0-360 and range 0-100%
    as appropriate
  • Prints the colorspace values in the appropriate format
  • Optionally creates and displays a color swatch for the specifice color

This is equivalent to the following IM commands for the RGB components.

  • rgb=`convert xc:"$color" -colorspace RGB txt:- |\
    tail -n 1 |\
    sed -n 's/ *//g; s/^.*[:][(]\(.*\)[)].*[\#].*$/\1/p'`
  • RR=`echo "$rgb" | cut -d, -f1`
  • GG=`echo "$rgb" | cut -d, -f2`
  • BB=`echo "$rgb" | cut -d, -f3`
  • AA=`echo "$rgb" | cut -d, -f4`
  • RRV=`convert xc: -format "%[fx:255*$RR/quantumrange]" info:`
  • GGV=`convert xc: -format "%[fx:255*$GG/quantumrange]" info:`
  • BBV=`convert xc: -format "%[fx:255*$BB/quantumrange]" info:`
  • RRP=`convert xc: -format "%[fx:100*$RR/quantumrange]" info:`
  • GGP=`convert xc: -format "%[fx:100*$GG/quantumrange]" info:`
  • BBP=`convert xc: -format "%[fx:100*$BB/quantumrange]" info:`
  • [ "$AA" != "" ] && AAV=`convert xc: -format "%[fx:$AA/quantumrange]" info:`
  • if [ "$AA" = "" ]; then
    echo "rgb($RRV,$GGV,$BBV)"
    echo "rgb($RRP%,$GGP%,$BBP%)"
    else
    echo "rgba($RR,$GG,$BB,$AAV)"
    echo "rgba($RRP%,$GGP%,$BBP%,$AAV)"
    fi
  • if [ "$show" = "yes" ]; then
    convert -respect-parenthesis \( -size "$dimensions" xc:"$color" \) \
    -colorspace rgb -background white -fill black -font Helvetica \
    -pointsize 10 label:"$color" -gravity south -append -alpha off show:
    fi