Skip to content

Instantly share code, notes, and snippets.

@yodeski
Forked from nmandery/hexcolor_to_mapserver.sql
Created November 14, 2012 04:15
Show Gist options
  • Save yodeski/4070220 to your computer and use it in GitHub Desktop.
Save yodeski/4070220 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