Skip to content

Instantly share code, notes, and snippets.

@nmandery
Created March 29, 2011 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nmandery/892033 to your computer and use it in GitHub Desktop.
Save nmandery/892033 to your computer and use it in GitHub Desktop.
convert hex colors to mapserver format in postgresql
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