123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470(*********************************************************************************)(* OCaml-Stk *)(* *)(* Copyright (C) 2023-2024 INRIA All rights reserved. *)(* Author: Maxence Guesdon, INRIA Saclay *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** Colors. *)openTsdltypet=int32moduleMap=Map.Make(Int32)letcompare=Int32.compareletppppft=Format.fprintfppf"0x%08lx"tletto_int8sn32=letn=Int32.to_intn32inletr=(nlsr24)land255inletg=(nlsr16)land255inletb=(nlsr8)land255inleta=nland255in(r,g,b,a)letof_rgba=letfn=Int32.of_int(max0(minn255))infunrgba->Int32.(logor(shift_left(fr)24)(logor(shift_left(fg)16)(logor(shift_left(fb)8)(fa))))letof_rgba_0_1rgba=letfx=letn=truncate(x*.255.)inmax0(min255n)inof_rgba(fr)(fg)(fb)(fa)letto_sdl_colorn=let(r,g,b,a)=to_int8sninSdl.Color.create~r~g~b~aletof_hexa=letof_small_hexs=letb=Buffer.create8infori=0to3doBuffer.add_charbs.[i];Buffer.add_charbs.[i];done;Buffer.contentsbinfuns->letlen=String.lengthsinlets=matchlenwith|3->of_small_hex(s^"f")|4->of_small_hexs|6->s^"ff"|_->sinlets=Printf.sprintf"0x%s"sinInt32.of_stringslettransparent=Int32.zerolettransparent_int8s=to_int8stransparentlettransparent_sdl=let(r,g,b,a)=transparent_int8sinSdl.Color.create~r~g~b~aletaliceblue:t=0xf0f8fffflletantiquewhite:t=0xfaebd7fflletaqua:t=0x00fffffflletaquamarine:t=0x7fffd4fflletazure:t=0xf0fffffflletbeige:t=0xf5f5dcfflletbisque:t=0xffe4c4fflletblack:t=0x000000fflletblanchedalmond:t=0xffebcdfflletblue:t=0x0000fffflletblueviolet:t=0x8a2be2fflletbrown:t=0xa52a2afflletburlywood:t=0xdeb887fflletcadetblue:t=0x5f9ea0fflletchartreuse:t=0x7fff00fflletchocolate:t=0xd2691efflletcoral:t=0xff7f50fflletcornflowerblue:t=0x6495edfflletcornsilk:t=0xfff8dcfflletcrimson:t=0xdc143cfflletcyan:t=0x00fffffflletdarkblue:t=0x00008bfflletdarkcyan:t=0x008b8bfflletdarkgoldenrod:t=0xb8860bfflletdarkgray:t=0xa9a9a9fflletdarkgreen:t=0x006400fflletdarkgrey:t=0xa9a9a9fflletdarkkhaki:t=0xbdb76bfflletdarkmagenta:t=0x8b008bfflletdarkolivegreen:t=0x556b2ffflletdarkorange:t=0xff8c00fflletdarkorchid:t=0x9932ccfflletdarkred:t=0x8b0000fflletdarksalmon:t=0xe9967afflletdarkseagreen:t=0x8fbc8ffflletdarkslateblue:t=0x483d8bfflletdarkslategray:t=0x2f4f4ffflletdarkslategrey:t=0x2f4f4ffflletdarkturquoise:t=0x00ced1fflletdarkviolet:t=0x9400d3fflletdeeppink:t=0xff1493fflletdeepskyblue:t=0x00bffffflletdimgray:t=0x696969fflletdimgrey:t=0x696969fflletdodgerblue:t=0x1e90fffflletfirebrick:t=0xb22222fflletfloralwhite:t=0xfffaf0fflletforestgreen:t=0x228b22fflletfuchsia:t=0xff00fffflletgainsboro:t=0xdcdcdcfflletghostwhite:t=0xf8f8fffflletgold:t=0xffd700fflletgoldenrod:t=0xdaa520fflletgray:t=0x808080fflletgreen:t=0x008000fflletgreenyellow:t=0xadff2ffflletgrey:t=0x808080ffllethoneydew:t=0xf0fff0ffllethotpink:t=0xff69b4fflletindianred:t=0xcd5c5cfflletindigo:t=0x4b0082fflletivory:t=0xfffff0fflletkhaki:t=0xf0e68cfflletlavender:t=0xe6e6fafflletlavenderblush:t=0xfff0f5fflletlawngreen:t=0x7cfc00fflletlemonchiffon:t=0xfffacdfflletlightblue:t=0xadd8e6fflletlightcoral:t=0xf08080fflletlightcyan:t=0xe0fffffflletlightgoldenrodyellow:t=0xfafad2fflletlightgray:t=0xd3d3d3fflletlightgreen:t=0x90ee90fflletlightgrey:t=0xd3d3d3fflletlightpink:t=0xffb6c1fflletlightsalmon:t=0xffa07afflletlightseagreen:t=0x20b2aafflletlightskyblue:t=0x87cefafflletlightslategray:t=0x778899fflletlightslategrey:t=0x778899fflletlightsteelblue:t=0xb0c4defflletlightyellow:t=0xffffe0fflletlime:t=0x00ff00fflletlimegreen:t=0x32cd32fflletlinen:t=0xfaf0e6fflletmagenta:t=0xff00fffflletmaroon:t=0x800000fflletmediumaquamarine:t=0x66cdaafflletmediumblue:t=0x0000cdfflletmediumorchid:t=0xba55d3fflletmediumpurple:t=0x9370dbfflletmediumseagreen:t=0x3cb371fflletmediumslateblue:t=0x7b68eefflletmediumspringgreen:t=0x00fa9afflletmediumturquoise:t=0x48d1ccfflletmediumvioletred:t=0xc71585fflletmidnightblue:t=0x191970fflletmintcream:t=0xf5fffafflletmistyrose:t=0xffe4e1fflletmoccasin:t=0xffe4b5fflletnavajowhite:t=0xffdeadfflletnavy:t=0x000080fflletoldlace:t=0xfdf5e6fflletolive:t=0x808000fflletolivedrab:t=0x6b8e23fflletorange:t=0xffa500fflletorangered:t=0xff4500fflletorchid:t=0xda70d6fflletpalegoldenrod:t=0xeee8aafflletpalegreen:t=0x98fb98fflletpaleturquoise:t=0xafeeeefflletpalevioletred:t=0xdb7093fflletpapayawhip:t=0xffefd5fflletpeachpuff:t=0xffdab9fflletperu:t=0xcd853ffflletpink:t=0xffc0cbfflletplum:t=0xdda0ddfflletpowderblue:t=0xb0e0e6fflletpurple:t=0x800080fflletred:t=0xff0000fflletrosybrown:t=0xbc8f8ffflletroyalblue:t=0x4169e1fflletsaddlebrown:t=0x8b4513fflletsalmon:t=0xfa8072fflletsandybrown:t=0xf4a460fflletseagreen:t=0x2e8b57fflletseashell:t=0xfff5eefflletsienna:t=0xa0522dfflletsilver:t=0xc0c0c0fflletskyblue:t=0x87ceebfflletslateblue:t=0x6a5acdfflletslategray:t=0x708090fflletslategrey:t=0x708090fflletsnow:t=0xfffafafflletspringgreen:t=0x00ff7ffflletsteelblue:t=0x4682b4ffllettan:t=0xd2b48cfflletteal:t=0x008080fflletthistle:t=0xd8bfd8ffllettomato:t=0xff6347fflletturquoise:t=0x40e0d0fflletviolet:t=0xee82eefflletwheat:t=0xf5deb3fflletwhite:t=0xfffffffflletwhitesmoke:t=0xf5f5f5fflletyellow:t=0xffff00fflletyellowgreen:t=0x9acd32ffl(* The list of predefined named colors. *)letnamed_colors=[aliceblue,"aliceblue";antiquewhite,"antiquewhite";aqua,"aqua";aquamarine,"aquamarine";azure,"azure";beige,"beige";bisque,"bisque";black,"black";blanchedalmond,"blanchedalmond";blue,"blue";blueviolet,"blueviolet";brown,"brown";burlywood,"burlywood";cadetblue,"cadetblue";chartreuse,"chartreuse";chocolate,"chocolate";coral,"coral";cornflowerblue,"cornflowerblue";cornsilk,"cornsilk";crimson,"crimson";cyan,"cyan";darkblue,"darkblue";darkcyan,"darkcyan";darkgoldenrod,"darkgoldenrod";darkgray,"darkgray";darkgreen,"darkgreen";darkgrey,"darkgrey";darkkhaki,"darkkhaki";darkmagenta,"darkmagenta";darkolivegreen,"darkolivegreen";darkorange,"darkorange";darkorchid,"darkorchid";darkred,"darkred";darksalmon,"darksalmon";darkseagreen,"darkseagreen";darkslateblue,"darkslateblue";darkslategray,"darkslategray";darkslategrey,"darkslategrey";darkturquoise,"darkturquoise";darkviolet,"darkviolet";deeppink,"deeppink";deepskyblue,"deepskyblue";dimgray,"dimgray";dimgrey,"dimgrey";dodgerblue,"dodgerblue";firebrick,"firebrick";floralwhite,"floralwhite";forestgreen,"forestgreen";fuchsia,"fuchsia";gainsboro,"gainsboro";ghostwhite,"ghostwhite";gold,"gold";goldenrod,"goldenrod";gray,"gray";green,"green";greenyellow,"greenyellow";grey,"grey";honeydew,"honeydew";hotpink,"hotpink";indianred,"indianred";indigo,"indigo";ivory,"ivory";khaki,"khaki";lavender,"lavender";lavenderblush,"lavenderblush";lawngreen,"lawngreen";lemonchiffon,"lemonchiffon";lightblue,"lightblue";lightcoral,"lightcoral";lightcyan,"lightcyan";lightgoldenrodyellow,"lightgoldenrodyellow";lightgray,"lightgray";lightgreen,"lightgreen";lightgrey,"lightgrey";lightpink,"lightpink";lightsalmon,"lightsalmon";lightseagreen,"lightseagreen";lightskyblue,"lightskyblue";lightslategray,"lightslategray";lightslategrey,"lightslategrey";lightsteelblue,"lightsteelblue";lightyellow,"lightyellow";lime,"lime";limegreen,"limegreen";linen,"linen";magenta,"magenta";maroon,"maroon";mediumaquamarine,"mediumaquamarine";mediumblue,"mediumblue";mediumorchid,"mediumorchid";mediumpurple,"mediumpurple";mediumseagreen,"mediumseagreen";mediumslateblue,"mediumslateblue";mediumspringgreen,"mediumspringgreen";mediumturquoise,"mediumturquoise";mediumvioletred,"mediumvioletred";midnightblue,"midnightblue";mintcream,"mintcream";mistyrose,"mistyrose";moccasin,"moccasin";navajowhite,"navajowhite";navy,"navy";oldlace,"oldlace";olive,"olive";olivedrab,"olivedrab";orange,"orange";orangered,"orangered";orchid,"orchid";palegoldenrod,"palegoldenrod";palegreen,"palegreen";paleturquoise,"paleturquoise";palevioletred,"palevioletred";papayawhip,"papayawhip";peachpuff,"peachpuff";peru,"peru";pink,"pink";plum,"plum";powderblue,"powderblue";purple,"purple";red,"red";rosybrown,"rosybrown";royalblue,"royalblue";saddlebrown,"saddlebrown";salmon,"salmon";sandybrown,"sandybrown";seagreen,"seagreen";seashell,"seashell";sienna,"sienna";silver,"silver";skyblue,"skyblue";slateblue,"slateblue";slategray,"slategray";slategrey,"slategrey";snow,"snow";springgreen,"springgreen";steelblue,"steelblue";tan,"tan";teal,"teal";thistle,"thistle";tomato,"tomato";turquoise,"turquoise";violet,"violet";wheat,"wheat";white,"white";whitesmoke,"whitesmoke";yellow,"yellow";yellowgreen,"yellowgreen";transparent,"transparent";]letcolor_by_name=refSmap.emptyletname_by_color=refMap.emptyletto_string?(as_name=true)c=ifnotas_namethenPrintf.sprintf"0x%08lx"celsematchMap.find_optc!name_by_colorwith|None->Printf.sprintf"0x%08lx"c|Somename->nameletregisternamec=(matchSmap.find_optname!color_by_namewith|None->()|Somec0->Log.warn(funm->m"Color name %S was previously used for %s (replacing with %s)"name(to_string~as_name:falsec0)(to_string~as_name:falsec)));(matchMap.find_optc!name_by_colorwith|None->()|Somename0->Log.warn(funm->m"Color %s was previously named %S (replacing with %S)"(to_string~as_name:falsec)name0name));color_by_name:=Smap.addnamec!color_by_name;name_by_color:=Map.addcname!name_by_colorlet()=List.iter(fun(c,name)->registernamec)named_colorsletregistered()=Smap.bindings!color_by_nameletof_strings=matchSmap.find_opts!color_by_namewith|Somec->c|None->letlen=String.lengthsintryiflen>1&&String.gets0='#'thenof_hexa(String.subs1(len-1))elseraiseNot_foundwith|_->Log.warn(funm->m"invalid color %S"s);blackletrandom()=letcolors=Array.of_list(registered())inletlen=Array.lengthcolorsinsndcolors.(Random.intlen)letocf_wrapper:tOcf.wrapper=letto_json?(with_doc=false)c=`String(to_stringc)inletfrom_json?def=function|`Strings->letlen=String.lengthsiniflen>2&&String.gets0='0'&&(matchString.gets1with'x'|'X'->true|_->false)thenmatchInt32.of_string_optswith|None->0x000000FFl|Somen->nelseof_strings|json->Ocf.invalid_valuejsoninOcf.Wrapper.maketo_jsonfrom_json