interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / crates / allen-design-tokens / design_tokens.om

1164 lines · 21839 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/allen-design-tokens/design_tokens.om: Pure-Omega Allen DESIGN.md token parser,
3note reference resolver, WCAG 2.2 luminance/contrast checker, and integer-only C header emitter.
4note Converted from aienux crates/allen-design-tokens/src/{lib,main}.rs and tests/exports.rs.
5note Compose with: si/directives/om2.om aienux/crates/allen-design-tokens/design_tokens.om ...
6
7const ADT_OK int 0
8const ADT_ERR_NO_FRONT int -1
9const ADT_ERR_UNCLOSED_FRONT int -2
10const ADT_ERR_UNCLOSED_BLOCK int -3
11const ADT_ERR_BAD_INDENT int -4
12const ADT_ERR_BAD_LINE int -5
13const ADT_ERR_BAD_KEY int -6
14const ADT_ERR_UNCLOSED_QUOTE int -7
15const ADT_ERR_DUPLICATE int -8
16const ADT_ERR_REF_LOOP int -9
17const ADT_ERR_REF_MISSING int -10
18const ADT_ERR_CONTRAST_FMT int -11
19const ADT_ERR_CONTRAST_COLOR int -12
20const ADT_ERR_CONTRAST_ALPHA int -13
21const ADT_ERR_CAPACITY int -14
22
23const ADT_VAL_COLOR uchar 1
24const ADT_VAL_DIM uchar 2
25const ADT_VAL_RATIO uchar 3
26const ADT_VAL_BEZIER uchar 4
27const ADT_VAL_TEXT uchar 5
28
29const ADT_MAX_TOKENS ulong 64
30const ADT_MAX_PATH ulong 64
31const ADT_MAX_RAW ulong 64
32const ADT_MAX_DEPTH ulong 6
33
34note Token table
35global adt_cnt ulong 1
36global adt_path uchar 4096
37global adt_path_len ulong 64
38global adt_raw uchar 4096
39global adt_raw_len ulong 64
40
41note YAML indent stack (up to 6 levels, 24 bytes per key)
42global adt_stk_cnt ulong 1
43global adt_stk_ind ulong 6
44global adt_stk_key uchar 144
45global adt_stk_klen ulong 6
46
47note Typed value output buffer:
48note 0=kind; Color: 1=r,2=g,3=b; Dim: adt_vf[0]=num, adt_vunit/len; Ratio: adt_vf[0]=num; Bezier: adt_vf[0..4]
49global adt_vkind uchar 1
50global adt_vrgb uchar 3
51global adt_vf f64 4
52global adt_vunit uchar 16
53global adt_vunit_len ulong 1
54
55note Contrast results table (up to 16 pairs):
56note 0=ratio, 1=floor, 2=alpha (in adt_cr_f), adt_cr_gated, adt_cr_pass
57global adt_cr_cnt ulong 1
58global adt_cr_ratio f64 16
59global adt_cr_floor f64 16
60global adt_cr_alpha f64 16
61global adt_cr_gated uchar 16
62global adt_cr_pass uchar 16
63
64text adt_fence_open "```allen-tokens"
65text adt_fence_close "```"
66text adt_bez_pfx "cubic-bezier("
67text adt_col_pfx "colors."
68text adt_da_pfx "drawn-alpha."
69text adt_kta_pfx "kernel-today-alpha."
70text adt_con_pfx "contrast."
71text adt_on_sep "-on-"
72text adt_def_pfx "#define ALLEN_"
73
74fn adt_init to void
75put adt_cnt 0 0
76put adt_stk_cnt 0 0
77put adt_cr_cnt 0 0
78end
79
80fn adt_slice_eq a view uchar * b view uchar * to bool
81if ne len a len b
82ret false
83end
84for i ulong len a bound 128
85if ne get a i get b i
86ret false
87end
88end
89ret true
90end
91
92fn adt_find_token path view uchar * plen ulong to long
93n = get adt_cnt 0
94for i ulong n bound 64
95if eq get adt_path_len i plen
96base = mul i ADT_MAX_PATH
97ok = true
98for j ulong plen bound 64
99if ne get adt_path add base j get path j
100ok = false
101break
102end
103end
104if ok
105ret long i
106end
107end
108end
109ret -1
110end
111
112fn adt_has_group_prefix grp view uchar * glen ulong to bool
113n = get adt_cnt 0
114for i ulong n bound 64
115pl = get adt_path_len i
116if gt pl add glen 1
117base = mul i ADT_MAX_PATH
118if eq get adt_path add base glen 46
119ok = true
120for j ulong glen bound 64
121if ne get adt_path add base j get grp j
122ok = false
123break
124end
125end
126if ok
127ret true
128end
129end
130end
131end
132ret false
133end
134
135fn adt_is_space c uchar to bool
136if eq c 32
137ret true
138end
139if eq c 9
140ret true
141end
142if eq c 13
143ret true
144end
145ret false
146end
147
148fn adt_hex_val c uchar to int
149if and ge c 48 le c 57
150ret int sub c 48
151end
152if and ge c 97 le c 102
153ret int add sub c 97 10
154end
155if and ge c 65 le c 70
156ret int add sub c 65 10
157end
158ret -1
159end
160
161note Parse decimal f64 from buf[off..off+n); sets out_val[0], returns true if valid
162fn adt_parse_f64 buf view uchar * off ulong n ulong out_val edit f64 * to bool
163if eq n 0
164ret false
165end
166i = ulong 0
167negf = false
168if eq get buf off 45
169negf = true
170i = 1
171if eq n 1
172ret false
173end
174end
175val = f64 0.0
176fdiv = f64 0.0
177seen_dig = false
178while lt i n bound 64
179c = get buf add off i
180if and ge c 48 le c 57
181seen_dig = true
182d = f64 sub c 48
183if gt fdiv 0.0f64
184fdiv = mul fdiv 10.0f64
185val = add val div d fdiv
186else
187val = add mul val 10.0f64 d
188end
189else
190if eq c 46
191if gt fdiv 0.0f64
192ret false
193end
194fdiv = 1.0f64
195else
196ret false
197end
198end
199i = add i 1
200end
201if not seen_dig
202ret false
203end
204if negf
205val = neg val
206end
207put out_val 0 val
208ret true
209end
210
211fn adt_try_color raw view uchar * rlen ulong to bool
212if ne rlen 7
213ret false
214end
215if ne get raw 0 35
216ret false
217end
218for k ulong 3
219hi = adt_hex_val get raw add 1 mul k 2
220lo = adt_hex_val get raw add 2 mul k 2
221if or lt hi 0 lt lo 0
222ret false
223end
224put adt_vrgb k uchar or shl hi 4 lo
225end
226put adt_vkind 0 ADT_VAL_COLOR
227ret true
228end
229
230fn adt_try_bezier raw view uchar * rlen ulong to bool
231if le rlen 14
232ret false
233end
234if ne get raw sub rlen 1 41
235ret false
236end
237if not adt_slice_eq part raw 0 13 adt_bez_pfx
238ret false
239end
240pos = ulong 13
241endp = sub rlen 1
242fv = new f64 1 0.0f64
243for k ulong 4
244while lt pos endp bound 64
245if adt_is_space get raw pos
246pos = add pos 1
247else
248break
249end
250end
251start = pos
252while lt pos endp bound 64
253if eq get raw pos 44
254break
255end
256pos = add pos 1
257end
258tok_end = pos
259while gt tok_end start bound 64
260if adt_is_space get raw sub tok_end 1
261tok_end = sub tok_end 1
262else
263break
264end
265end
266if not adt_parse_f64 raw start sub tok_end start fv
267ret false
268end
269put adt_vf k get fv 0
270if lt k 3
271if ge pos endp
272ret false
273end
274if ne get raw pos 44
275ret false
276end
277pos = add pos 1
278end
279end
280if ne pos endp
281ret false
282end
283put adt_vkind 0 ADT_VAL_BEZIER
284ret true
285end
286
287fn adt_try_dim raw view uchar * rlen ulong to bool
288sp = ulong 0
289while lt sp rlen bound 64
290c = get raw sp
291is_d = and ge c 48 le c 57
292is_m = or eq c 46 eq c 45
293if not or is_d is_m
294break
295end
296sp = add sp 1
297end
298if or eq sp 0 ge sp rlen
299ret false
300end
301fv = new f64 1 0.0f64
302if not adt_parse_f64 raw 0 sp fv
303ret false
304end
305ulen = sub rlen sp
306if gt ulen 16
307ret false
308end
309for j ulong ulen bound 16
310uc = get raw add sp j
311is_u = and ge uc 65 le uc 90
312is_l = and ge uc 97 le uc 122
313if not or is_u is_l
314ret false
315end
316end
317put adt_vf 0 get fv 0
318put adt_vunit_len 0 ulen
319for j ulong ulen bound 16
320put adt_vunit j get raw add sp j
321end
322put adt_vkind 0 ADT_VAL_DIM
323ret true
324end
325
326note Classify a raw value into Color / Bezier / Ratio / Dim / Text (matches lib.rs value())
327fn adt_classify raw view uchar * rlen ulong to uchar
328put adt_vunit_len 0 0
329if adt_try_color raw rlen
330ret ADT_VAL_COLOR
331end
332if adt_try_bezier raw rlen
333ret ADT_VAL_BEZIER
334end
335fv = new f64 1 0.0f64
336if adt_parse_f64 raw 0 rlen fv
337put adt_vf 0 get fv 0
338put adt_vkind 0 ADT_VAL_RATIO
339ret ADT_VAL_RATIO
340end
341if adt_try_dim raw rlen
342ret ADT_VAL_DIM
343end
344put adt_vkind 0 ADT_VAL_TEXT
345ret ADT_VAL_TEXT
346end
347
348note Scratch buffer for building dotted path
349global adt_pbuf uchar 64
350global adt_pbuf_len ulong 1
351
352fn adt_build_stack_prefix to long
353sc = get adt_stk_cnt 0
354ppos = ulong 0
355for lvl ulong sc bound 6
356lk = get adt_stk_klen lvl
357if gt add add ppos lk 1 ADT_MAX_PATH
358ret long ADT_ERR_CAPACITY
359end
360sbase = mul lvl 24
361for j ulong lk bound 24
362put adt_pbuf add ppos j get adt_stk_key add sbase j
363end
364ppos = add ppos lk
365put adt_pbuf ppos 46
366ppos = add ppos 1
367end
368ret long ppos
369end
370
371fn adt_build_path src view uchar * kstart ulong klen ulong to int
372r = adt_build_stack_prefix
373if lt r 0
374ret int r
375end
376ppos = ulong r
377if gt add ppos klen ADT_MAX_PATH
378ret ADT_ERR_CAPACITY
379end
380for j ulong klen bound 24
381put adt_pbuf add ppos j get src add kstart j
382end
383put adt_pbuf_len 0 add ppos klen
384ret ADT_OK
385end
386
387note Insert a single key/value into token table using current indent stack
388fn adt_store_kv src view uchar * kstart ulong klen ulong vstart ulong vlen ulong to int
389rc = adt_build_path src kstart klen
390if ne rc ADT_OK
391ret rc
392end
393ppos = get adt_pbuf_len 0
394pbuf = new uchar 64 0
395for j ulong ppos bound 64
396put pbuf j get adt_pbuf j
397end
398if ge adt_find_token view pbuf ppos 0
399ret ADT_ERR_DUPLICATE
400end
401idx = get adt_cnt 0
402if ge idx ADT_MAX_TOKENS
403ret ADT_ERR_CAPACITY
404end
405base = mul idx ADT_MAX_PATH
406for j ulong ppos bound 64
407put adt_path add base j get pbuf j
408end
409put adt_path_len idx ppos
410base = mul idx ADT_MAX_RAW
411for j ulong vlen bound 64
412put adt_raw add base j get src add vstart j
413end
414put adt_raw_len idx vlen
415put adt_cnt 0 add idx 1
416ret ADT_OK
417end
418
419fn adt_push_stack src view uchar * ind ulong kstart ulong klen ulong to int
420sc = get adt_stk_cnt 0
421if ge sc ADT_MAX_DEPTH
422ret ADT_ERR_CAPACITY
423end
424put adt_stk_ind sc ind
425put adt_stk_klen sc klen
426sbase = mul sc 24
427for j ulong klen bound 24
428put adt_stk_key add sbase j get src add kstart j
429end
430put adt_stk_cnt 0 add sc 1
431ret ADT_OK
432end
433
434fn adt_parse_yaml_val src view uchar * kstart ulong klen ulong vstart ulong endp ulong to int
435vs = vstart
436while lt vs endp bound 256
437if adt_is_space get src vs
438vs = add vs 1
439else
440break
441end
442end
443vlen = sub endp vs
444if eq get src vs 34
445if lt vlen 2
446ret ADT_ERR_UNCLOSED_QUOTE
447end
448if ne get src sub endp 1 34
449ret ADT_ERR_UNCLOSED_QUOTE
450end
451vs = add vs 1
452vlen = sub vlen 2
453end
454if gt vlen ADT_MAX_RAW
455ret ADT_ERR_CAPACITY
456end
457ret adt_store_kv src kstart klen vs vlen
458end
459
460fn adt_parse_yaml_line src view uchar * lstart ulong llen ulong to int
461endp = add lstart llen
462while gt endp lstart bound 256
463if adt_is_space get src sub endp 1
464endp = sub endp 1
465else
466break
467end
468end
469if eq endp lstart
470ret ADT_OK
471end
472ind = ulong 0
473while lt add lstart ind endp bound 256
474if eq get src add lstart ind 32
475ind = add ind 1
476else
477break
478end
479end
480if eq add lstart ind endp
481ret ADT_OK
482end
483if eq get src add lstart ind 35
484ret ADT_OK
485end
486if ne rem ind 2 0
487ret ADT_ERR_BAD_INDENT
488end
489
490cpos = add lstart ind
491while lt cpos endp bound 256
492if eq get src cpos 58
493break
494end
495cpos = add cpos 1
496end
497if eq cpos endp
498ret ADT_ERR_BAD_LINE
499end
500kstart = add lstart ind
501kend = cpos
502while gt kend kstart bound 256
503if adt_is_space get src sub kend 1
504kend = sub kend 1
505else
506break
507end
508end
509if or eq kend kstart gt sub kend kstart 24
510ret ADT_ERR_BAD_KEY
511end
512for j ulong sub kend kstart bound 24
513if eq get src add kstart j 32
514ret ADT_ERR_BAD_KEY
515end
516end
517
518sc = get adt_stk_cnt 0
519while gt sc 0 bound 8
520if ge get adt_stk_ind sub sc 1 ind
521sc = sub sc 1
522else
523break
524end
525end
526put adt_stk_cnt 0 sc
527if ne mul sc 2 ind
528ret ADT_ERR_BAD_INDENT
529end
530
531vstart = add cpos 1
532while lt vstart endp bound 256
533if adt_is_space get src vstart
534vstart = add vstart 1
535else
536break
537end
538end
539if eq vstart endp
540ret adt_push_stack src ind kstart sub kend kstart
541end
542ret adt_parse_yaml_val src kstart sub kend kstart vstart endp
543end
544
545note Resolve {group.name} references up to 8 hops
546fn adt_resolve_all to int
547n = get adt_cnt 0
548cur = new uchar 64 0
549tbuf = new uchar 64 0
550for i ulong n bound 64
551clen = get adt_raw_len i
552rbase = mul i ADT_MAX_RAW
553for j ulong clen bound 64
554put cur j get adt_raw add rbase j
555end
556hops = ulong 0
557while ge clen 2 bound 10
558if not and eq get cur 0 123 eq get cur sub clen 1 125
559break
560end
561hops = add hops 1
562if gt hops 8
563ret ADT_ERR_REF_LOOP
564end
565tlen = sub clen 2
566for j ulong tlen bound 64
567put tbuf j get cur add j 1
568end
569tidx = adt_find_token view tbuf tlen
570if ge tidx 0
571clen = get adt_raw_len ulong tidx
572tb = mul ulong tidx ADT_MAX_RAW
573for j ulong clen bound 64
574put cur j get adt_raw add tb j
575end
576else
577if adt_has_group_prefix view tbuf tlen
578break
579else
580ret ADT_ERR_REF_MISSING
581end
582end
583end
584put adt_raw_len i clen
585for j ulong clen bound 64
586put adt_raw add rbase j get cur j
587end
588end
589ret ADT_OK
590end
591
592fn adt_is_dashes src view uchar * off ulong llen ulong to bool
593e = add off llen
594while gt e off bound 64
595if adt_is_space get src sub e 1
596e = sub e 1
597else
598break
599end
600end
601if ne sub e off 3
602ret false
603end
604if ne get src off 45
605ret false
606end
607if ne get src add off 1 45
608ret false
609end
610if ne get src add off 2 45
611ret false
612end
613ret true
614end
615
616note Parse a complete DESIGN.md document: front matter + ```allen-tokens blocks + resolve
617fn adt_parse doc view uchar * dlen ulong to int
618adt_init
619if lt dlen 4
620ret ADT_ERR_NO_FRONT
621end
622if ne get doc 0 45
623ret ADT_ERR_NO_FRONT
624end
625if ne get doc 1 45
626ret ADT_ERR_NO_FRONT
627end
628if ne get doc 2 45
629ret ADT_ERR_NO_FRONT
630end
631if ne get doc 3 10
632ret ADT_ERR_NO_FRONT
633end
634pos = ulong 4
635in_front = true
636in_block = false
637put adt_stk_cnt 0 0
638
639while lt pos dlen bound 4096
640lstart = pos
641while lt pos dlen bound 256
642if eq get doc pos 10
643break
644end
645pos = add pos 1
646end
647llen = sub pos lstart
648if lt pos dlen
649pos = add pos 1
650end
651
652if in_front
653if adt_is_dashes doc lstart llen
654in_front = false
655put adt_stk_cnt 0 0
656else
657rc = adt_parse_yaml_line doc lstart llen
658if ne rc ADT_OK
659ret rc
660end
661end
662else
663if in_block
664e = add lstart llen
665while and gt e lstart adt_is_space get doc sub e 1 bound 64
666e = sub e 1
667end
668if adt_slice_eq part doc lstart sub e lstart adt_fence_close
669in_block = false
670put adt_stk_cnt 0 0
671else
672rc = adt_parse_yaml_line doc lstart llen
673if ne rc ADT_OK
674ret rc
675end
676end
677else
678e = add lstart llen
679while and gt e lstart adt_is_space get doc sub e 1 bound 64
680e = sub e 1
681end
682if adt_slice_eq part doc lstart sub e lstart adt_fence_open
683in_block = true
684put adt_stk_cnt 0 0
685end
686end
687end
688end
689
690if in_front
691ret ADT_ERR_UNCLOSED_FRONT
692end
693if in_block
694ret ADT_ERR_UNCLOSED_BLOCK
695end
696ret adt_resolve_all
697end
698
699note --- WCAG 2.2 relative luminance & contrast ratio ---
700
701note ln(x) for x in (0, 2] via range reduction + atanh series (9 terms -> < 1e-12 error)
702fn adt_ln_f64 x f64 to f64
703v = x
704k = int 0
705while lt v 0.7071067811865475f64 bound 32
706v = mul v 2.0f64
707k = sub k 1
708end
709while gt v 1.4142135623730951f64 bound 32
710v = mul v 0.5f64
711k = add k 1
712end
713y = div sub v 1.0f64 add v 1.0f64
714y2 = mul y y
715term = y
716acc = y
717for m ulong 9
718term = mul term y2
719denom = f64 add mul add m 1 2 1
720acc = add acc div term denom
721end
722ret add mul 2.0f64 acc mul f64 k 0.6931471805599453f64
723end
724
725note exp(x) via x/ln2 = n + f and 10-term Taylor
726fn adt_exp_f64 x f64 to f64
727t = mul x 1.4426950408889634f64
728n = long t
729if gt f64 n t
730n = sub n 1
731end
732f = mul sub t f64 n 0.6931471805599453f64
733sum = f64 1.0
734term = f64 1.0
735for k ulong 12
736term = div mul term f f64 add k 1
737sum = add sum term
738end
739ebits = shl ulong add n 1023 52
740scale = bitcast f64 ebits
741ret mul sum scale
742end
743
744fn adt_srgb_lin c uchar to f64
745v = div f64 c 255.0f64
746if le v 0.04045f64
747ret div v 12.92f64
748end
749base = div add v 0.055f64 1.055f64
750ret adt_exp_f64 mul 2.4f64 adt_ln_f64 base
751end
752
753fn adt_luminance r uchar g uchar b uchar to f64
754lr = adt_srgb_lin r
755lg = adt_srgb_lin g
756lb = adt_srgb_lin b
757ret add add mul 0.2126f64 lr mul 0.7152f64 lg mul 0.0722f64 lb
758end
759
760fn adt_contrast r1 uchar g1 uchar b1 uchar r2 uchar g2 uchar b2 uchar to f64
761la = adt_luminance r1 g1 b1
762lb = adt_luminance r2 g2 b2
763hi = la
764lo = lb
765if gt lb la
766hi = lb
767lo = la
768end
769ret div add hi 0.05f64 add lo 0.05f64
770end
771
772fn adt_round_i64 v f64 to long
773if ge v 0.0f64
774ret long add v 0.5f64
775end
776ret long sub v 0.5f64
777end
778
779fn adt_blend_ch fg uchar bg uchar alpha f64 to uchar
780f = f64 fg
781b = f64 bg
782v = add b mul sub f b alpha
783r = adt_round_i64 v
784if lt r 0
785ret 0
786end
787if gt r 255
788ret 255
789end
790ret uchar r
791end
792
793fn adt_q_fixed v f64 scale f64 to long
794ret adt_round_i64 mul v scale
795end
796
797note Lookup colors.<name> into rgb_out[3]
798fn adt_color_of name view uchar * nlen ulong rgb_out edit uchar 3 to int
799key = new uchar 64 0
800if gt add 7 nlen 64
801ret ADT_ERR_CONTRAST_COLOR
802end
803for i ulong 7
804put key i get adt_col_pfx i
805end
806for i ulong nlen bound 48
807put key add 7 i get name i
808end
809idx = adt_find_token view key add 7 nlen
810if lt idx 0
811ret ADT_ERR_CONTRAST_COLOR
812end
813rl = get adt_raw_len ulong idx
814rb = mul ulong idx ADT_MAX_RAW
815raw = new uchar 64 0
816for i ulong rl bound 64
817put raw i get adt_raw add rb i
818end
819kind = adt_classify view raw rl
820if ne kind ADT_VAL_COLOR
821ret ADT_ERR_CONTRAST_COLOR
822end
823put rgb_out 0 get adt_vrgb 0
824put rgb_out 1 get adt_vrgb 1
825put rgb_out 2 get adt_vrgb 2
826ret ADT_OK
827end
828
829fn adt_lookup_drawn_alpha fg view uchar * flen ulong out_a edit f64 1 to int
830put out_a 0 1.0f64
831if gt add 12 flen 64
832ret ADT_OK
833end
834da_key = new uchar 64 0
835for j ulong 12
836put da_key j get adt_da_pfx j
837end
838for j ulong flen bound 48
839put da_key add 12 j get fg j
840end
841da_idx = adt_find_token view da_key add 12 flen
842if lt da_idx 0
843ret ADT_OK
844end
845drl = get adt_raw_len ulong da_idx
846drb = mul ulong da_idx ADT_MAX_RAW
847rbuf = new uchar 64 0
848for j ulong drl bound 64
849put rbuf j get adt_raw add drb j
850end
851if ne adt_classify view rbuf drl ADT_VAL_RATIO
852ret ADT_ERR_CONTRAST_ALPHA
853end
854a = get adt_vf 0
855if or lt a 0.0f64 gt a 1.0f64
856ret ADT_ERR_CONTRAST_ALPHA
857end
858put out_a 0 a
859ret ADT_OK
860end
861
862global adt_fg_tmp uchar 64
863global adt_fg_len ulong 1
864global adt_bg_tmp uchar 64
865global adt_bg_len ulong 1
866
867fn adt_blend_contrast fg_rgb view uchar 3 bg_rgb view uchar 3 alpha f64 to f64
868br = adt_blend_ch get fg_rgb 0 get bg_rgb 0 alpha
869bg_c = adt_blend_ch get fg_rgb 1 get bg_rgb 1 alpha
870bb = adt_blend_ch get fg_rgb 2 get bg_rgb 2 alpha
871ret adt_contrast br bg_c bb get bg_rgb 0 get bg_rgb 1 get bg_rgb 2
872end
873
874fn adt_eval_contrast_pair flr f64 to int
875fg_buf = new uchar 64 0
876flen = get adt_fg_len 0
877for j ulong flen bound 48
878put fg_buf j get adt_fg_tmp j
879end
880bg_buf = new uchar 64 0
881blen = get adt_bg_len 0
882for j ulong blen bound 48
883put bg_buf j get adt_bg_tmp j
884end
885fg_rgb = new uchar 3 0
886bg_rgb = new uchar 3 0
887rc = adt_color_of view fg_buf flen fg_rgb
888if ne rc ADT_OK
889ret rc
890end
891rc = adt_color_of view bg_buf blen bg_rgb
892if ne rc ADT_OK
893ret rc
894end
895aval = new f64 1 1.0f64
896rc = adt_lookup_drawn_alpha view fg_buf flen aval
897if ne rc ADT_OK
898ret rc
899end
900ratio = adt_blend_contrast view fg_rgb view bg_rgb get aval 0
901ci = get adt_cr_cnt 0
902if ge ci 16
903ret ADT_ERR_CAPACITY
904end
905put adt_cr_ratio ci ratio
906put adt_cr_floor ci flr
907put adt_cr_alpha ci get aval 0
908put adt_cr_gated ci 1
909if ge ratio flr
910put adt_cr_pass ci 1
911else
912put adt_cr_pass ci 0
913end
914put adt_cr_cnt 0 add ci 1
915ret ADT_OK
916end
917
918fn adt_check_one_contrast i ulong to int
919pl = get adt_path_len i
920if le pl 9
921ret ADT_OK
922end
923pbase = mul i ADT_MAX_PATH
924for j ulong 9
925if ne get adt_path add pbase j get adt_con_pfx j
926ret ADT_OK
927end
928end
929pair_len = sub pl 9
930sep_at = ulong 999
931if ge pair_len 6
932for k ulong sub pair_len 3 bound 64
933off = add pbase add 9 k
934if eq get adt_path off 45
935if eq get adt_path add off 1 111
936if eq get adt_path add off 2 110
937if eq get adt_path add off 3 45
938sep_at = k
939break
940end
941end
942end
943end
944end
945end
946if eq sep_at 999
947ret ADT_ERR_CONTRAST_FMT
948end
949put adt_fg_len 0 sep_at
950for j ulong sep_at bound 48
951put adt_fg_tmp j get adt_path add pbase add 9 j
952end
953bg_len = sub sub pair_len sep_at 4
954put adt_bg_len 0 bg_len
955for j ulong bg_len bound 48
956put adt_bg_tmp j get adt_path add pbase add add 13 sep_at j
957end
958rl = get adt_raw_len i
959rb = mul i ADT_MAX_RAW
960rbuf = new uchar 64 0
961for j ulong rl bound 64
962put rbuf j get adt_raw add rb j
963end
964if ne adt_classify view rbuf rl ADT_VAL_RATIO
965ret ADT_ERR_CONTRAST_FMT
966end
967ret adt_eval_contrast_pair get adt_vf 0
968end
969
970note Check every contrast.<fg>-on-<bg> pair against its floor
971fn adt_check_contrast to int
972put adt_cr_cnt 0 0
973n = get adt_cnt 0
974for i ulong n bound 64
975rc = adt_check_one_contrast i
976if ne rc ADT_OK
977ret rc
978end
979end
980ret ADT_OK
981end
982
983note --- Integer-only C header line emitter (CK_RGB / Q8 / Q15 / Q16) ---
984
985fn adt_to_ident_char c uchar to uchar
986if and ge c 97 le c 122
987ret sub c 32
988end
989if and ge c 65 le c 90
990ret c
991end
992if and ge c 48 le c 57
993ret c
994end
995ret 95
996end
997
998fn adt_write_hex2 dst edit uchar * pos ulong v uchar to ulong
999hi = shr v 4
1000lo = and v 0xf
1001if lt hi 10
1002put dst pos add 48 hi
1003else
1004put dst pos add 55 hi
1005end
1006if lt lo 10
1007put dst add pos 1 add 48 lo
1008else
1009put dst add pos 1 add 55 lo
1010end
1011ret add pos 2
1012end
1013
1014fn adt_write_i64 dst edit uchar * pos ulong v long to ulong
1015p = pos
1016u = ulong 0
1017if lt v 0
1018put dst p 45
1019p = add p 1
1020u = ulong neg v
1021else
1022u = ulong v
1023end
1024if eq u 0
1025put dst p 48
1026ret add p 1
1027end
1028tmp = new uchar 24 0
1029k = ulong 0
1030while gt u 0 bound 24
1031put tmp k uchar add 48 rem u 10
1032u = div u 10
1033k = add k 1
1034end
1035for i ulong k bound 24
1036put dst add p i get tmp sub sub k 1 i
1037end
1038ret add p k
1039end
1040
1041fn adt_emit_c_dim dst edit uchar * pos ulong to long
1042p = pos
1043put dst p 95
1044p = add p 1
1045ul = get adt_vunit_len 0
1046for j ulong ul bound 16
1047put dst add p j adt_to_ident_char get adt_vunit j
1048end
1049p = add p ul
1050v = get adt_vf 0
1051iv = long v
1052if ge v 0.0f64
1053if eq f64 iv v
1054put dst p 32
1055p = adt_write_i64 dst add p 1 iv
1056put dst p 117
1057put dst add p 1 10
1058ret long add p 2
1059end
1060end
1061put dst p 95
1062put dst add p 1 88
1063put dst add p 2 49
1064put dst add p 3 48
1065put dst add p 4 48
1066put dst add p 5 48
1067put dst add p 6 32
1068p = adt_write_i64 dst add p 7 adt_q_fixed v 1000.0f64
1069put dst p 10
1070ret long add p 1
1071end
1072
1073fn adt_emit_c_ratio dst edit uchar * pos ulong to long
1074p = pos
1075v = get adt_vf 0
1076iv = long v
1077if ge fabs v 2.0f64
1078if eq f64 iv v
1079put dst p 32
1080p = adt_write_i64 dst add p 1 iv
1081put dst p 10
1082ret long add p 1
1083end
1084end
1085put dst p 95
1086put dst add p 1 81
1087put dst add p 2 49
1088put dst add p 3 54
1089put dst add p 4 32
1090p = adt_write_i64 dst add p 5 adt_q_fixed v 65536.0f64
1091put dst p 10
1092ret long add p 1
1093end
1094
1095fn adt_emit_c_prefix idx ulong dst edit uchar * to long
1096if ge idx get adt_cnt 0
1097ret -1
1098end
1099if lt len dst 128
1100ret -1
1101end
1102pl = get adt_path_len idx
1103pbase = mul idx ADT_MAX_PATH
1104for j ulong 14
1105put dst j get adt_def_pfx j
1106end
1107for j ulong pl bound 64
1108put dst add 14 j adt_to_ident_char get adt_path add pbase j
1109end
1110ret long add 14 pl
1111end
1112
1113note Emit integer-only C #define for token idx into dst; returns bytes written
1114fn adt_emit_c_token idx ulong dst edit uchar * to long
1115p0 = adt_emit_c_prefix idx dst
1116if lt p0 0
1117ret -1
1118end
1119p = ulong p0
1120rl = get adt_raw_len idx
1121rbase = mul idx ADT_MAX_RAW
1122raw = new uchar 64 0
1123for j ulong rl bound 64
1124put raw j get adt_raw add rbase j
1125end
1126kind = adt_classify view raw rl
1127
1128if eq kind ADT_VAL_COLOR
1129put dst p 32
1130put dst add p 1 48
1131put dst add p 2 120
1132p = add p 3
1133p = adt_write_hex2 dst p get adt_vrgb 0
1134p = adt_write_hex2 dst p get adt_vrgb 1
1135p = adt_write_hex2 dst p get adt_vrgb 2
1136put dst p 117
1137put dst add p 1 10
1138ret long add p 2
1139end
1140
1141if eq kind ADT_VAL_DIM
1142ret adt_emit_c_dim dst p
1143end
1144
1145if eq kind ADT_VAL_RATIO
1146ret adt_emit_c_ratio dst p
1147end
1148
1149if eq kind ADT_VAL_BEZIER
1150put dst p 95
1151put dst add p 1 88
1152put dst add p 2 49
1153put dst add p 3 95
1154put dst add p 4 81
1155put dst add p 5 49
1156put dst add p 6 54
1157put dst add p 7 32
1158p = adt_write_i64 dst add p 8 adt_q_fixed get adt_vf 0 65536.0f64
1159put dst p 10
1160ret long add p 1
1161end
1162
1163ret 0
1164end