#!/usr/bin/env bash

color1=$1
color2=$2
color3=$3

# Associative array to store color values
declare -A colors=(
  ["black"]=0
  ["brown"]=1
  ["red"]=2
  ["orange"]=3
  ["yellow"]=4
  ["green"]=5
  ["blue"]=6
  ["violet"]=7
  ["grey"]=8
  ["white"]=9
)

value="${colors[$color1]}${colors[$color2]}"
zeros="${colors[$color3]}"

# Calculate the value with zeros
value_with_zeros="$value"
for (( i = 0; i < zeros; i++ )); do
  value_with_zeros+="0"
done

# Check if the first digit is zero and omit it if needed
if [[ ${value_with_zeros:0:1} == "0" ]]; then
  value_with_zeros=${value_with_zeros:1}
fi

# Determine the metric prefix
giga=1000000000
mega=1000000
kilo=1000
if (( value_with_zeros >= $giga )) && (( value_with_zeros % $giga == 0)); then
  prefix="gigaohms"
  value_with_zeros=$(( value_with_zeros / $giga ))
elif (( value_with_zeros >= $mega )) && (( value_with_zeros % $mega == 0)); then
  prefix="megaohms"
 value_with_zeros=$(( value_with_zeros / $mega ))
elif (( value_with_zeros >= $kilo )) && (( value_with_zeros % $kilo == 0)); then
  prefix="kiloohms"
  value_with_zeros=$(( value_with_zeros / $kilo ))
else
  prefix="ohms"
fi

if [[ -z "${colors[$color1]}" || -z "${colors[$color2]}" || -z "${colors[$color3]}" ]]; then
  return
else
    echo "$value_with_zeros $prefix"
fi
