hex2rgb.sh 426 B

123456789101112131415161718192021222324
  1. #!/bin/bash
  2. # Check if input is provided
  3. if [ -z "$1" ]; then
  4. echo "Usage: $0 <hex_color>"
  5. echo "Example: $0 #FF00FF or $0 FF00FF"
  6. exit 1
  7. fi
  8. # Remove # if present
  9. hex=${1#"#"}
  10. # Validate hex length
  11. if [ ${#hex} -ne 6 ]; then
  12. echo "Error: Hex color must be 6 characters long (RRGGBB)"
  13. exit 1
  14. fi
  15. # Convert hex to RGB
  16. r=$((16#${hex:0:2}))
  17. g=$((16#${hex:2:2}))
  18. b=$((16#${hex:4:2}))
  19. echo "RGB: $r, $g, $b"