Enhance your color picker to:

  • Convert to multiple color formats (hex, rgb(a), hsl(a))
  • Display the terminal color preview
  • Get a list of standard color names (e.g. Cornflower Blue, Crimson) that are visually close
color_picker_and_pastel_with_notes.png

For all these we just need next two tools and a little of shell scripting.

Zenity

Zenity enables you to create the following types of simple dialog: calendar, notification icon, color selection,…

Zenity is usually pre-installed in standard Ubuntu installations (especially those with GNOME desktop). If missing run sudo apt install zenity

Pastel

Pastel is a command-line tool to generate, analyze, convert and manipulate colors.

To install Pastel just update Rust (rustup update stable) and run cargo install pastel. For details read the docs.

Color picker enhancement

Steps:

  • Paste the code below in ~/.bashrc and run in terminal color_picker
  • Zenit color selection GUI will open
  • Pick a color and click “Select”
  • The therminal will show awesome!
Zenit_color_selection.PNGcolor_picker__output.PNG
color_check_valid_format() {
  # Example usage: color_check_valid_format "#FF0000"
  
  # Check if any argument was supplied
  local color="$1"
  if [ -z "$color" ]; then
    echo "No argument supplied"
    return 1
  fi

  # Check if it's a valid color code
  if pastel color "$color" >/dev/null 2>&1; then
    return 0
  else
    echo "This is NOT a valid color format: $color"
    return 1
  fi
}
color_picker() {
  # Open zenity color picker, then pick a color and click "Select"
  color=$(zenity --color-selection --title="Pick a Color" --color="green" 2>/dev/null)

  # Check if user clicked "Cancel"
  if [[ -z "$color" ]]; then
      echo "No color selected."
      return 1
  fi

  # Check if a valid color is provided
  if ! color_check_valid_format "$color"; then
    return 1
  fi

  # Convert HEX to RGB, etc.
  pastel color "$color"
}

Bonus

Check the color format. Screenshot:

color_check_format__outputs.png

Did you notice that the hex color is previewed in the terminal? Just use a Syntax Highlighting tool, for example:

Code:

color_check_format() {
  # Example usage: 
  #   color_check_format "rgba(207, 196, 26, 0.737)"
  #   color_check_format "rgb(207, 196, 26)"
  #   color_check_format "hsla(56, 77.7%, 45.7%, 0.737)"
  #   color_check_format "hsl(56, 77.7%, 45.7%)"
  #   color_check_format "#FF0000"
  #   color_check_format "red"
  #   color_check_format "Invalid-format"
  
  # Check if a valid color format is provided
  local color="$1"
  if ! color_check_valid_format "$color"; then
    return 1
  fi

  # Check the format
  if [[ "$color" =~ ^rgba ]]; then echo "RGBA format"
  elif [[ "$color" =~ ^rgb ]]; then echo "RGB format"
  elif [[ "$color" =~ ^hsla ]]; then echo "HSLA format"
  elif [[ "$color" =~ ^hsl ]]; then echo "HSL format"
  elif [[ "$color" =~ ^# ]]; then echo "HEX format"
  else echo "Other [valid] format"
  fi
}