Created
March 29, 2011 08:59
-
-
Save nmandery/892033 to your computer and use it in GitHub Desktop.
convert hex colors to mapserver format in postgresql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace function public.hexcolor_to_mapserver(hexcolor text) returns text as | |
$body$ | |
declare | |
thexcolor text; | |
color_r int; | |
color_g int; | |
color_b int; | |
begin | |
thexcolor := trim(both ' ' from hexcolor); | |
if not thexcolor ~ '^#[0-9a-fA-F]{6}$' then | |
raise exception 'the given value does not appear to be a valid hex color representation'; | |
end if; | |
-- need to used excute for dynamic casting :( | |
execute 'select x''' || substring(thexcolor,2,2) || '''::int;' into color_r; | |
execute 'select x''' || substring(thexcolor,4,2) || '''::int;' into color_g; | |
execute 'select x''' || substring(thexcolor,6,2) || '''::int;' into color_b; | |
return color_r::text || ' ' || color_g::text || ' ' || color_b::text; | |
end | |
$body$ LANGUAGE plpgsql VOLATILE; | |
COMMENT ON FUNCTION public.hexcolor_to_mapserver(text) IS 'convert a hex color represantation to a mapserver compatible one with a string containing three integer values.'; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment