Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file color.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463(** Colors. *)openTsdl(** A color is a int32, with bytes representing red, green, blue and alpha (opacity).*)typet=int32letcompare=Int32.compareletppppft=Format.fprintfppf"%#lx"t(** [to_int8s color] returns [(red, green, blue, alpha)] components of color.*)letto_int8sn32=letn=Int32.to_intn32inletr=(nlsr24)land255inletg=(nlsr16)land255inletb=(nlsr8)land255inleta=nland255in(r,g,b,a)(** [of_rgba r g b a] returns a color from the given components.*)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))))(** [of_rgba_0_1 r g b a] returns a color from the given components as floats.
Each component must be between [0.] and [1.] and is multiplicated by 255 to
get the value on one byte for this component.
Out of bounds values are corrected to be at least 0 and at most 255. *)letof_rgba_0_1rgba=letfx=letn=truncate(x*.255.)inmax0(min255n)inof_rgba(fr)(fg)(fb)(fa)(** [sdl_color_of_int32 n] creates a {!Tsdl.Sdl.color}. *)letsdl_color_of_int32n=let(r,g,b,a)=to_int8sninSdl.Color.create~r~g~b~a(** Returns a color from a hexadecimal string representation.
With each character being [0]..[9] or [a|A]..[f|F], accepted forms are:
{ul
{- ["rgb"], mapped to (rr,gg,bb,FF),}
{- ["rgba"], mapped to (rr,gg,bb,aa),}
{- ["rrggbb"], mapped to (rr,gg,bb,FF),}
{- any other string [s] which can be parsed by [Int32.of_string ("0x"^s)].}
}
Raises [Failure] if the representation is invalid.*)letof_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_strings(** The transparent color. *)lettransparent=Int32.zero(** Transparent color as [(r,g,b,a)] bytes. *)lettransparent_int8s=to_int8stransparent(** Transparent SDL color. *)lettransparent_color=let(r,g,b,a)=transparent_int8sinSdl.Color.create~r~g~b~a(** {2 Named colors}
Thes named colors corresponds to the
{{:https://www.w3.org/TR/css-color-3/#svg-color}SVG color specification}.
*)letaliceblue: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(** [of_name s] returns the color associated to the given string
(case-sensitive). Else, if [s] begin with ['#'], the rest of the
string is parsed with {!of_hexa}. If it fails or if the string
does not start with ['#'], the {!black} color is returned. *)letof_name=function|"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|s->letlen=String.lengthsintryiflen>1&&String.gets0='#'thenof_hexa(String.subs1(len-1))elseraiseNot_foundwith|_->Log.warn(funm->m"invalid color %S"s);black(** The list of named colors. *)letnamed_colors=[aliceblue;antiquewhite;aqua;aquamarine;azure;beige;bisque;black;blanchedalmond;blue;blueviolet;brown;burlywood;cadetblue;chartreuse;chocolate;coral;cornflowerblue;cornsilk;crimson;cyan;darkblue;darkcyan;darkgoldenrod;darkgray;darkgreen;darkgrey;darkkhaki;darkmagenta;darkolivegreen;darkorange;darkorchid;darkred;darksalmon;darkseagreen;darkslateblue;darkslategray;darkslategrey;darkturquoise;darkviolet;deeppink;deepskyblue;dimgray;dimgrey;dodgerblue;firebrick;floralwhite;forestgreen;fuchsia;gainsboro;ghostwhite;gold;goldenrod;gray;green;greenyellow;grey;honeydew;hotpink;indianred;indigo;ivory;khaki;lavender;lavenderblush;lawngreen;lemonchiffon;lightblue;lightcoral;lightcyan;lightgoldenrodyellow;lightgray;lightgreen;lightgrey;lightpink;lightsalmon;lightseagreen;lightskyblue;lightslategray;lightslategrey;lightsteelblue;lightyellow;lime;limegreen;linen;magenta;maroon;mediumaquamarine;mediumblue;mediumorchid;mediumpurple;mediumseagreen;mediumslateblue;mediumspringgreen;mediumturquoise;mediumvioletred;midnightblue;mintcream;mistyrose;moccasin;navajowhite;navy;oldlace;olive;olivedrab;orange;orangered;orchid;palegoldenrod;palegreen;paleturquoise;palevioletred;papayawhip;peachpuff;peru;pink;plum;powderblue;purple;red;rosybrown;royalblue;saddlebrown;salmon;sandybrown;seagreen;seashell;sienna;silver;skyblue;slateblue;slategray;slategrey;snow;springgreen;steelblue;tan;teal;thistle;tomato;turquoise;violet;wheat;white;whitesmoke;yellow;yellowgreen;transparent;](** [random ()] returns a random color from the list of named colors. *)letrandom=letcolors=Array.of_listnamed_colorsinletlen=Array.lengthcolorsinfun()->colors.(Random.intlen)(** {!Ocf.wrapper} for a color. *)letocf_wrapper:tOcf.wrapper=letto_json?(with_doc=false)c=`String(Printf.sprintf"%#lx"c)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_names|json->Ocf.invalid_valuejsoninOcf.Wrapper.maketo_jsonfrom_json