Skip to content

Instantly share code, notes, and snippets.

@mhulse
Last active October 27, 2024 03:40
Show Gist options
  • Save mhulse/b11e568260fb8c3aa2a8 to your computer and use it in GitHub Desktop.
Save mhulse/b11e568260fb8c3aa2a8 to your computer and use it in GitHub Desktop.

Revisions

  1. mhulse revised this gist Mar 12, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions fromhex.bash
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # fromhex A52A2A
    # fromhex "#A52A2A"
    # BLUE_VIOLET=$(fromhex "#8A2BE2")
    # http://unix.stackexchange.com/a/269085/67282
    function fromhex() {
    hex=$1
  2. mhulse created this gist Mar 12, 2016.
    13 changes: 13 additions & 0 deletions fromhex.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # fromhex A52A2A
    # fromhex "#A52A2A"
    # http://unix.stackexchange.com/a/269085/67282
    function fromhex() {
    hex=$1
    if [[ $hex == "#"* ]]; then
    hex=$(echo $1 | awk '{print substr($0,2)}')
    fi
    r=$(printf '0x%0.2s' "$hex")
    g=$(printf '0x%0.2s' ${hex#??})
    b=$(printf '0x%0.2s' ${hex#????})
    echo -e `printf "%03d" "$(((r<75?0:(r-35)/40)*6*6+(g<75?0:(g-35)/40)*6+(b<75?0:(b-35)/40)+16))"`
    }