-- Color changer script for use with web development in BBEdit
-- Hacked together by Paul Burney (http://www.paulburney.com/) in April 2005
-- with various pieces of code found on mailing lists
set original_color to selection of window 1 as string
if original_color = "" then
beep
else
--display dialog change_case(original_color, "lower") as string
set original_color_rgb to hex2rgb(change_case(original_color, "upper"))
--display dialog original_color_rgb as string
set new_color to choose color default color original_color_rgb
--display dialog new_color as string
set new_color_hex to rgb2hex(new_color)
--display dialog new_color_hex as string
set selection of window 1 to new_color_hex as string
end if
property hexdigits : "0123456789ABCDEF"
on rgb2hex(RGBcolor)
return (makeHex((item 1 of RGBcolor) div 256, 2)) & (makeHex((item 2 of RGBcolor) div 256, 2)) & (makeHex((item 3 of RGBcolor) div 256, 2))
end rgb2hex
on hex2rgb(HEXcolor)
set HEXcolor to two_chunkize(HEXcolor)
set converted to {}
repeat with z in HEXcolor
set dec to 0
set z to reverse of (z's text items)
repeat with i from 1 to count z
try
set x to (offset of (z's item i) in hexdigits) - 1
on error
exit repeat
end try
set dec to dec + (x * (16 ^ (i - 1))) * 256
end repeat
set sum to dec div 256
set dec to dec + sum -- adjust plain decimal to base 256
set converted to converted & dec
end repeat
return converted
end hex2rgb
on makeHex(dec, nDigits)
--Converts an arbitrary unsigned decimal integer to a hexadecimal value
set s to ""
repeat nDigits times
set s to character ((dec mod 16) + 1) of hexdigits & s
set dec to (dec div 16)
end repeat
return s
end makeHex
on two_chunkize(HEXcolor)
set chunkizado to {}
set i to 1
repeat
try
set chunkizado to chunkizado & (HEXcolor's items i thru (i + 1) as string)
on error
exit repeat
end try
set i to i + 2
end repeat
return chunkizado
end two_chunkize
property letters_uc : (characters of "ABCDEFGHIJKLMNOPQRSTUVWXYZÆŒØ?")
property letters_lc : (characters of "abcdefghijklmnopqrstuvwxyzæœø?")
on change_case(the_string, convert_case)
if convert_case = "lower" then
set search_chars to letters_uc
set replace_chars to letters_lc
else
set search_chars to letters_lc
set replace_chars to letters_uc
end if
set return_string to ""
repeat with i from 1 to (count of characters of the_string)
set string_char to (character i of the_string)
if search_chars contains string_char then
repeat with j from 1 to (count of search_chars)
if (item j of search_chars) = string_char then
set return_string to return_string & (item j of replace_chars)
exit repeat
end if
end repeat
else
set return_string to return_string & string_char
end if
end repeat
return return_string as string
end change_case