Skip to content

Instantly share code, notes, and snippets.

@ruda
Created October 11, 2017 01:12
Show Gist options
  • Save ruda/83e08894e5fe8915539f0cff226ca1f4 to your computer and use it in GitHub Desktop.
Save ruda/83e08894e5fe8915539f0cff226ca1f4 to your computer and use it in GitHub Desktop.
Show Color Picker and wait for user selection, then return the hex color number to the clipboard.
(*
* Show Color Picker and wait for user selection, then return the hex color number to the clipboard.
* For input, it can use a RGB color from the clipboard (valid formats: #RRGGBB or #RGB).
*
* Copyright © 2012-2017 Rudá Moura. All rights reserved.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*)
property lastColor : {65535, 65535, 65535}
set clr to lastColor
try
if class of (the clipboard) is text and (the clipboard) starts with "#" then
try
set clr to ConvertToRGBColor(the clipboard)
on error "Not a RGB color."
log "The clipboard does not have any valid RGB color, using last known color"
end try
end if
on error "Can’t make some data into the expected type."
log "Can't make the clipboard content into text, using last known color."
end try
try
set clr to choose color default color clr
on error "User canceled."
return
end try
set lastColor to clr
set hexcolor to ConvertToHexColor(clr)
set the clipboard to hexcolor
return hexcolor
(* Convert from 8-bit RGB in hexadecimal to 16-bit RGB *)
on ConvertToRGBColor(hexcolor)
if length of hexcolor is 4 then
set r to item 2 of hexcolor
set r to r & r
set g to item 3 of hexcolor
set g to g & g
set b to item 4 of hexcolor
set b to b & b
else if length of hexcolor is 7 then
set r to text 2 thru 3 of hexcolor
set g to text 4 thru 5 of hexcolor
set b to text 6 thru 7 of hexcolor
else
error "Not a RGB color."
end if
try
return {hex2dec(r) * 257, hex2dec(g) * 257, hex2dec(b) * 257} (* Note: 65535 / 257 = 255 *)
on error "Not a hex number."
error "Not a RGB color."
end try
end ConvertToRGBColor
(* Convert from 16-bit RGB to 8-bit RGB in hexadecimal *)
on ConvertToHexColor(rgb)
log rgb
set res to "#"
repeat with i from 1 to the count of rgb
set clr to ((item i of rgb) / 65535) * 255
set clr to dec2hex(clr)
set res to res & clr
end repeat
return res
end ConvertToHexColor
on hex2dec(hexdigits)
set hexdigits to reverse of characters of hexdigits as string
set dec to 0
repeat with i from 1 to the count of hexdigits
set h to item i of hexdigits
set dec to dec + (16 ^ (i - 1)) * (hexdigit2dec(h))
end repeat
return dec as integer
end hex2dec
on dec2hex(num)
set hi to (num div 16) as integer
set lo to num mod 16
set hi to hexdigit(hi)
set lo to hexdigit(lo)
return hi & lo
end dec2hex
on hexdigit(num)
if num < 10 then
return ASCII character ((ASCII number "0") + num)
else
set num to num - 10
return ASCII character ((ASCII number "A") + num)
end if
end hexdigit
on hexdigit2dec(hexdigit)
set anum to ASCII number hexdigit
if anum ≥ (ASCII number "0") and anum ≤ (ASCII number "9") then
return hexdigit as integer
else if anum ≥ (ASCII number "a") and anum ≤ (ASCII number "f") then
return (anum - (ASCII number "a")) + 10
else if anum ≥ (ASCII number "A") and anum ≤ (ASCII number "F") then
return (anum - (ASCII number "A")) + 10
else
error "Not a hex number."
end if
end hexdigit2dec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment