Dung.Dang@gmail.com | My favorites | Profile | Sign out
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
Part of the Processing project - http://processing.org

Copyright (c) 2004-09 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/

package processing.core;

import java.awt.*;
import java.util.HashMap;


/**
* Main graphics and rendering context, as well as the base API implementation for processing "core".
* Use this class if you need to draw into an off-screen graphics buffer.
* A PGraphics object can be constructed with the <b>createGraphics()</b> function.
* The <b>beginDraw()</b> and <b>endDraw()</b> methods (see above example) are necessary to set up the buffer and to finalize it.
* The fields and methods for this class are extensive;
* for a complete list visit the developer's reference: http://dev.processing.org/reference/core/
* =advanced
* Main graphics and rendering context, as well as the base API implementation.
*
* <h2>Subclassing and initializing PGraphics objects</h2>
* Starting in release 0149, subclasses of PGraphics are handled differently.
* The constructor for subclasses takes no parameters, instead a series of
* functions are called by the hosting PApplet to specify its attributes.
* <ul>
* <li>setParent(PApplet) - is called to specify the parent PApplet.
* <li>setPrimary(boolean) - called with true if this PGraphics will be the
* primary drawing surface used by the sketch, or false if not.
* <li>setPath(String) - called when the renderer needs a filename or output
* path, such as with the PDF or DXF renderers.
* <li>setSize(int, int) - this is called last, at which point it's safe for
* the renderer to complete its initialization routine.
* </ul>
* The functions were broken out because of the growing number of parameters
* such as these that might be used by a renderer, yet with the exception of
* setSize(), it's not clear which will be necessary. So while the size could
* be passed in to the constructor instead of a setSize() function, a function
* would still be needed that would notify the renderer that it was time to
* finish its initialization. Thus, setSize() simply does both.
*
* <h2>Know your rights: public vs. private methods</h2>
* Methods that are protected are often subclassed by other renderers, however
* they are not set 'public' because they shouldn't be part of the user-facing
* public API accessible from PApplet. That is, we don't want sketches calling
* textModeCheck() or vertexTexture() directly.
*
* <h2>Handling warnings and exceptions</h2>
* Methods that are unavailable generally show a warning, unless their lack of
* availability will soon cause another exception. For instance, if a method
* like getMatrix() returns null because it is unavailable, an exception will
* be thrown stating that the method is unavailable, rather than waiting for
* the NullPointerException that will occur when the sketch tries to use that
* method. As of release 0149, warnings will only be shown once, and exceptions
* have been changed to warnings where possible.
*
* <h2>Using xxxxImpl() for subclassing smoothness</h2>
* The xxxImpl() methods are generally renderer-specific handling for some
* subset if tasks for a particular function (vague enough for you?) For
* instance, imageImpl() handles drawing an image whose x/y/w/h and u/v coords
* have been specified, and screen placement (independent of imageMode) has
* been determined. There's no point in all renderers implementing the
* <tt>if (imageMode == BLAH)</tt> placement/sizing logic, so that's handled
* by PGraphics, which then calls imageImpl() once all that is figured out.
*
* <h2>His brother PImage</h2>
* PGraphics subclasses PImage so that it can be drawn and manipulated in a
* similar fashion. As such, many methods are inherited from PGraphics,
* though many are unavailable: for instance, resize() is not likely to be
* implemented; the same goes for mask(), depending on the situation.
*
* <h2>What's in PGraphics, what ain't</h2>
* For the benefit of subclasses, as much as possible has been placed inside
* PGraphics. For instance, bezier interpolation code and implementations of
* the strokeCap() method (that simply sets the strokeCap variable) are
* handled here. Features that will vary widely between renderers are located
* inside the subclasses themselves. For instance, all matrix handling code
* is per-renderer: Java 2D uses its own AffineTransform, P2D uses a PMatrix2D,
* and PGraphics3D needs to keep continually update forward and reverse
* transformations. A proper (future) OpenGL implementation will have all its
* matrix madness handled by the card. Lighting also falls under this
* category, however the base material property settings (emissive, specular,
* et al.) are handled in PGraphics because they use the standard colorMode()
* logic. Subclasses should override methods like emissiveFromCalc(), which
* is a point where a valid color has been defined internally, and can be
* applied in some manner based on the calcXxxx values.
*
* <h2>What's in the PGraphics documentation, what ain't</h2>
* Some things are noted here, some things are not. For public API, always
* refer to the <a href="http://processing.org/reference">reference</A>
* on Processing.org for proper explanations. <b>No attempt has been made to
* keep the javadoc up to date or complete.</b> It's an enormous task for
* which we simply do not have the time. That is, it's not something that
* to be done once&mdash;it's a matter of keeping the multiple references
* synchronized (to say nothing of the translation issues), while targeting
* them for their separate audiences. Ouch.
*
* We're working right now on synchronizing the two references, so the website reference
* is generated from the javadoc comments. Yay.
*
* @webref rendering
* @instanceName graphics any object of the type PGraphics
* @usage Web &amp; Application
* @see processing.core.PApplet#createGraphics(int, int, String)
*/
public class PGraphics extends PImage implements PConstants {

// ........................................................

// width and height are already inherited from PImage


/// width minus one (useful for many calculations)
protected int width1;

/// height minus one (useful for many calculations)
protected int height1;

/// width * height (useful for many calculations)
public int pixelCount;

/// true if smoothing is enabled (read-only)
public boolean smooth = false;

// ........................................................

/// true if defaults() has been called a first time
protected boolean settingsInited;

/// set to a PGraphics object being used inside a beginRaw/endRaw() block
protected PGraphics raw;

// ........................................................

/** path to the file being saved for this renderer (if any) */
protected String path;

/**
* true if this is the main drawing surface for a particular sketch.
* This would be set to false for an offscreen buffer or if it were
* created any other way than size(). When this is set, the listeners
* are also added to the sketch.
*/
protected boolean primarySurface;

// ........................................................

/**
* Array of hint[] items. These are hacks to get around various
* temporary workarounds inside the environment.
* <p/>
* Note that this array cannot be static, as a hint() may result in a
* runtime change specific to a renderer. For instance, calling
* hint(DISABLE_DEPTH_TEST) has to call glDisable() right away on an
* instance of PGraphicsOpenGL.
* <p/>
* The hints[] array is allocated early on because it might
* be used inside beginDraw(), allocate(), etc.
*/
protected boolean[] hints = new boolean[HINT_COUNT];


////////////////////////////////////////////////////////////

// STYLE PROPERTIES

// Also inherits imageMode() and smooth() (among others) from PImage.

/** The current colorMode */
public int colorMode; // = RGB;

/** Max value for red (or hue) set by colorMode */
public float colorModeX; // = 255;

/** Max value for green (or saturation) set by colorMode */
public float colorModeY; // = 255;

/** Max value for blue (or value) set by colorMode */
public float colorModeZ; // = 255;

/** Max value for alpha set by colorMode */
public float colorModeA; // = 255;

/** True if colors are not in the range 0..1 */
boolean colorModeScale; // = true;

/** True if colorMode(RGB, 255) */
boolean colorModeDefault; // = true;

// ........................................................

// Tint color for images

/**
* True if tint() is enabled (read-only).
*
* Using tint/tintColor seems a better option for naming than
* tintEnabled/tint because the latter seems ugly, even though
* g.tint as the actual color seems a little more intuitive,
* it's just that g.tintEnabled is even more unintuitive.
* Same goes for fill and stroke, et al.
*/
public boolean tint;

/** tint that was last set (read-only) */
public int tintColor;

protected boolean tintAlpha;
protected float tintR, tintG, tintB, tintA;
protected int tintRi, tintGi, tintBi, tintAi;

// ........................................................

// Fill color

/** true if fill() is enabled, (read-only) */
public boolean fill;

/** fill that was last set (read-only) */
public int fillColor = 0xffFFFFFF;

protected boolean fillAlpha;
protected float fillR, fillG, fillB, fillA;
protected int fillRi, fillGi, fillBi, fillAi;

// ........................................................

// Stroke color

/** true if stroke() is enabled, (read-only) */
public boolean stroke;

/** stroke that was last set (read-only) */
public int strokeColor = 0xff000000;

protected boolean strokeAlpha;
protected float strokeR, strokeG, strokeB, strokeA;
protected int strokeRi, strokeGi, strokeBi, strokeAi;

// ........................................................

// Additional stroke properties

static protected final float DEFAULT_STROKE_WEIGHT = 1;
static protected final int DEFAULT_STROKE_JOIN = MITER;
static protected final int DEFAULT_STROKE_CAP = ROUND;

/**
* Last value set by strokeWeight() (read-only). This has a default
* setting, rather than fighting with renderers about whether that
* renderer supports thick lines.
*/
public float strokeWeight = DEFAULT_STROKE_WEIGHT;

/**
* Set by strokeJoin() (read-only). This has a default setting
* so that strokeJoin() need not be called by defaults,
* because subclasses may not implement it (i.e. PGraphicsGL)
*/
public int strokeJoin = DEFAULT_STROKE_JOIN;

/**
* Set by strokeCap() (read-only). This has a default setting
* so that strokeCap() need not be called by defaults,
* because subclasses may not implement it (i.e. PGraphicsGL)
*/
public int strokeCap = DEFAULT_STROKE_CAP;

// ........................................................

// Shape placement properties

// imageMode() is inherited from PImage

/** The current rect mode (read-only) */
public int rectMode;

/** The current ellipse mode (read-only) */
public int ellipseMode;

/** The current shape alignment mode (read-only) */
public int shapeMode;

/** The current image alignment (read-only) */
public int imageMode = CORNER;

// ........................................................

// Text and font properties

/** The current text font (read-only) */
public PFont textFont;

/** The current text align (read-only) */
public int textAlign = LEFT;

/** The current vertical text alignment (read-only) */
public int textAlignY = BASELINE;

/** The current text mode (read-only) */
public int textMode = MODEL;

/** The current text size (read-only) */
public float textSize;

/** The current text leading (read-only) */
public float textLeading;

// ........................................................

// Material properties

// PMaterial material;
// PMaterial[] materialStack;
// int materialStackPointer;

public float ambientR, ambientG, ambientB;
public float specularR, specularG, specularB;
public float emissiveR, emissiveG, emissiveB;
public float shininess;


// Style stack

static final int STYLE_STACK_DEPTH = 64;
PStyle[] styleStack = new PStyle[STYLE_STACK_DEPTH];
int styleStackDepth;


////////////////////////////////////////////////////////////


/** Last background color that was set, zero if an image */
public int backgroundColor = 0xffCCCCCC;

protected boolean backgroundAlpha;
protected float backgroundR, backgroundG, backgroundB, backgroundA;
protected int backgroundRi, backgroundGi, backgroundBi, backgroundAi;

// ........................................................

/**
* Current model-view matrix transformation of the form m[row][column],
* which is a "column vector" (as opposed to "row vector") matrix.
*/
// PMatrix matrix;
// public float m00, m01, m02, m03;
// public float m10, m11, m12, m13;
// public float m20, m21, m22, m23;
// public float m30, m31, m32, m33;

// static final int MATRIX_STACK_DEPTH = 32;
// float[][] matrixStack = new float[MATRIX_STACK_DEPTH][16];
// float[][] matrixInvStack = new float[MATRIX_STACK_DEPTH][16];
// int matrixStackDepth;

static final int MATRIX_STACK_DEPTH = 32;

// ........................................................

/**
* Java AWT Image object associated with this renderer. For P2D and P3D,
* this will be associated with their MemoryImageSource. For PGraphicsJava2D,
* it will be the offscreen drawing buffer.
*/
public Image image;

// ........................................................

// internal color for setting/calculating
protected float calcR, calcG, calcB, calcA;
protected int calcRi, calcGi, calcBi, calcAi;
protected int calcColor;
protected boolean calcAlpha;

/** The last RGB value converted to HSB */
int cacheHsbKey;
/** Result of the last conversion to HSB */
float[] cacheHsbValue = new float[3];

// ........................................................

/**
* Type of shape passed to beginShape(),
* zero if no shape is currently being drawn.
*/
protected int shape;

// vertices
public static final int DEFAULT_VERTICES = 512;
protected float vertices[][] =
new float[DEFAULT_VERTICES][VERTEX_FIELD_COUNT];
protected int vertexCount; // total number of vertices

// ........................................................

protected boolean bezierInited = false;
public int bezierDetail = 20;

// used by both curve and bezier, so just init here
protected PMatrix3D bezierBasisMatrix =
new PMatrix3D(-1, 3, -3, 1,
3, -6, 3, 0,
-3, 3, 0, 0,
1, 0, 0, 0);

//protected PMatrix3D bezierForwardMatrix;
protected PMatrix3D bezierDrawMatrix;

// ........................................................

protected boolean curveInited = false;
protected int curveDetail = 20;
public float curveTightness = 0;
// catmull-rom basis matrix, perhaps with optional s parameter
protected PMatrix3D curveBasisMatrix;
protected PMatrix3D curveDrawMatrix;

protected PMatrix3D bezierBasisInverse;
protected PMatrix3D curveToBezierMatrix;

// ........................................................

// spline vertices

protected float curveVertices[][];
protected int curveVertexCount;

// ........................................................

// precalculate sin/cos lookup tables [toxi]
// circle resolution is determined from the actual used radii
// passed to ellipse() method. this will automatically take any
// scale transformations into account too

// [toxi 031031]
// changed table's precision to 0.5 degree steps
// introduced new vars for more flexible code
static final protected float sinLUT[];
static final protected float cosLUT[];
static final protected float SINCOS_PRECISION = 0.5f;
static final protected int SINCOS_LENGTH = (int) (360f / SINCOS_PRECISION);
static {
sinLUT = new float[SINCOS_LENGTH];
cosLUT = new float[SINCOS_LENGTH];
for (int i = 0; i < SINCOS_LENGTH; i++) {
sinLUT[i] = (float) Math.sin(i * DEG_TO_RAD * SINCOS_PRECISION);
cosLUT[i] = (float) Math.cos(i * DEG_TO_RAD * SINCOS_PRECISION);
}
}

// ........................................................

/** The current font if a Java version of it is installed */
//protected Font textFontNative;

/** Metrics for the current native Java font */
//protected FontMetrics textFontNativeMetrics;

/** Last text position, because text often mixed on lines together */
protected float textX, textY, textZ;

/**
* Internal buffer used by the text() functions
* because the String object is slow
*/
protected char[] textBuffer = new char[8 * 1024];
protected char[] textWidthBuffer = new char[8 * 1024];

protected int textBreakCount;
protected int[] textBreakStart;
protected int[] textBreakStop;

// ........................................................

public boolean edge = true;

// ........................................................

/// normal calculated per triangle
static protected final int NORMAL_MODE_AUTO = 0;
/// one normal manually specified per shape
static protected final int NORMAL_MODE_SHAPE = 1;
/// normals specified for each shape vertex
static protected final int NORMAL_MODE_VERTEX = 2;

/// Current mode for normals, one of AUTO, SHAPE, or VERTEX
protected int normalMode;

/// Keep track of how many calls to normal, to determine the mode.
//protected int normalCount;

protected boolean autoNormal;

/** Current normal vector. */
public float normalX, normalY, normalZ;

// ........................................................

/**
* Sets whether texture coordinates passed to
* vertex() calls will be based on coordinates that are
* based on the IMAGE or NORMALIZED.
*/
public int textureMode;

/**
* Current horizontal coordinate for texture, will always
* be between 0 and 1, even if using textureMode(IMAGE).
*/
public float textureU;

/** Current vertical coordinate for texture, see above. */
public float textureV;

/** Current image being used as a texture */
public PImage textureImage;

// ........................................................

// [toxi031031] new & faster sphere code w/ support flexibile resolutions
// will be set by sphereDetail() or 1st call to sphere()
float sphereX[], sphereY[], sphereZ[];

/// Number of U steps (aka "theta") around longitudinally spanning 2*pi
public int sphereDetailU = 0;
/// Number of V steps (aka "phi") along latitudinally top-to-bottom spanning pi
public int sphereDetailV = 0;


//////////////////////////////////////////////////////////////

// INTERNAL


/**
* Constructor for the PGraphics object. Use this to ensure that
* the defaults get set properly. In a subclass, use this(w, h)
* as the first line of a subclass' constructor to properly set
* the internal fields and defaults.
*
*/
public PGraphics() {
}


public void setParent(PApplet parent) { // ignore
this.parent = parent;
}


/**
* Set (or unset) this as the main drawing surface. Meaning that it can
* safely be set to opaque (and given a default gray background), or anything
* else that goes along with that.
*/
public void setPrimary(boolean primary) { // ignore
this.primarySurface = primary;

// base images must be opaque (for performance and general
// headache reasons.. argh, a semi-transparent opengl surface?)
// use createGraphics() if you want a transparent surface.
if (primarySurface) {
format = RGB;
}
}


public void setPath(String path) { // ignore
this.path = path;
}


/**
* The final step in setting up a renderer, set its size of this renderer.
* This was formerly handled by the constructor, but instead it's been broken
* out so that setParent/setPrimary/setPath can be handled differently.
*
* Important that this is ignored by preproc.pl because otherwise it will
* override setSize() in PApplet/Applet/Component, which will 1) not call
* super.setSize(), and 2) will cause the renderer to be resized from the
* event thread (EDT), causing a nasty crash as it collides with the
* animation thread.
*/
public void setSize(int w, int h) { // ignore
width = w;
height = h;
width1 = width - 1;
height1 = height - 1;

allocate();
reapplySettings();
}


/**
* Allocate memory for this renderer. Generally will need to be implemented
* for all renderers.
*/
protected void allocate() { }


/**
* Handle any takedown for this graphics context.
* <p>
* This is called when a sketch is shut down and this renderer was
* specified using the size() command, or inside endRecord() and
* endRaw(), in order to shut things off.
*/
public void dispose() { // ignore
}



//////////////////////////////////////////////////////////////

// FRAME


/**
* Some renderers have requirements re: when they are ready to draw.
*/
public boolean canDraw() { // ignore
return true;
}


/**
* Sets the default properties for a PGraphics object. It should be called before anything is drawn into the object.
* =advanced
* <p/>
* When creating your own PGraphics, you should call this before
* drawing anything.
*
* @webref
* @brief Sets up the rendering context
*/
public void beginDraw() { // ignore
}


/**
* Finalizes the rendering of a PGraphics object so that it can be shown on screen.
* =advanced
* <p/>
* When creating your own PGraphics, you should call this when
* you're finished drawing.
*
* @webref
* @brief Finalizes the renderering context
*/
public void endDraw() { // ignore
}


public void flush() {
// no-op, mostly for P3D to write sorted stuff
}


protected void checkSettings() {
if (!settingsInited) defaultSettings();
}


/**
* Set engine's default values. This has to be called by PApplet,
* somewhere inside setup() or draw() because it talks to the
* graphics buffer, meaning that for subclasses like OpenGL, there
* needs to be a valid graphics context to mess with otherwise
* you'll get some good crashing action.
*
* This is currently called by checkSettings(), during beginDraw().
*/
protected void defaultSettings() { // ignore
// System.out.println("PGraphics.defaultSettings() " + width + " " + height);

noSmooth(); // 0149

colorMode(RGB, 255);
fill(255);
stroke(0);

// as of 0178, no longer relying on local versions of the variables
// being set, because subclasses may need to take extra action.
strokeWeight(DEFAULT_STROKE_WEIGHT);
strokeJoin(DEFAULT_STROKE_JOIN);
strokeCap(DEFAULT_STROKE_CAP);

// init shape stuff
shape = 0;

// init matrices (must do before lights)
//matrixStackDepth = 0;

rectMode(CORNER);
ellipseMode(DIAMETER);

autoNormal = true;

// no current font
textFont = null;
textSize = 12;
textLeading = 14;
textAlign = LEFT;
textMode = MODEL;

// if this fella is associated with an applet, then clear its background.
// if it's been created by someone else through createGraphics,
// they have to call background() themselves, otherwise everything gets
// a gray background (when just a transparent surface or an empty pdf
// is what's desired).
// this background() call is for the Java 2D and OpenGL renderers.
if (primarySurface) {
//System.out.println("main drawing surface bg " + getClass().getName());
background(backgroundColor);
}

settingsInited = true;
// defaultSettings() overlaps reapplySettings(), don't do both
//reapplySettings = false;
}


/**
* Re-apply current settings. Some methods, such as textFont(), require that
* their methods be called (rather than simply setting the textFont variable)
* because they affect the graphics context, or they require parameters from
* the context (e.g. getting native fonts for text).
*
* This will only be called from an allocate(), which is only called from
* size(), which is safely called from inside beginDraw(). And it cannot be
* called before defaultSettings(), so we should be safe.
*/
protected void reapplySettings() {
// System.out.println("attempting reapplySettings()");
if (!settingsInited) return; // if this is the initial setup, no need to reapply

// System.out.println(" doing reapplySettings");
// new Exception().printStackTrace(System.out);

colorMode(colorMode, colorModeX, colorModeY, colorModeZ);
if (fill) {
// PApplet.println(" fill " + PApplet.hex(fillColor));
fill(fillColor);
} else {
noFill();
}
if (stroke) {
stroke(strokeColor);

// The if() statements should be handled inside the functions,
// otherwise an actual reset/revert won't work properly.
//if (strokeWeight != DEFAULT_STROKE_WEIGHT) {
strokeWeight(strokeWeight);
//}
// if (strokeCap != DEFAULT_STROKE_CAP) {
strokeCap(strokeCap);
// }
// if (strokeJoin != DEFAULT_STROKE_JOIN) {
strokeJoin(strokeJoin);
// }
} else {
noStroke();
}
if (tint) {
tint(tintColor);
} else {
noTint();
}
if (smooth) {
smooth();
} else {
// Don't bother setting this, cuz it'll anger P3D.
noSmooth();
}
if (textFont != null) {
// System.out.println(" textFont in reapply is " + textFont);
// textFont() resets the leading, so save it in case it's changed
float saveLeading = textLeading;
textFont(textFont, textSize);
textLeading(saveLeading);
}
textMode(textMode);
textAlign(textAlign, textAlignY);
background(backgroundColor);

//reapplySettings = false;
}

// inherit from PImage
//public void resize(int wide, int high){ }

//////////////////////////////////////////////////////////////

// HINTS

/**
* Set various hints and hacks for the renderer. This is used to handle obscure rendering features that cannot be implemented in a consistent manner across renderers. Many options will often graduate to standard features instead of hints over time.
* <br><br>hint(ENABLE_OPENGL_4X_SMOOTH) - Enable 4x anti-aliasing for OpenGL. This can help force anti-aliasing if it has not been enabled by the user. On some graphics cards, this can also be set by the graphics driver's control panel, however not all cards make this available. This hint must be called immediately after the size() command because it resets the renderer, obliterating any settings and anything drawn (and like size(), re-running the code that came before it again).
* <br><br>hint(DISABLE_OPENGL_2X_SMOOTH) - In Processing 1.0, Processing always enables 2x smoothing when the OpenGL renderer is used. This hint disables the default 2x smoothing and returns the smoothing behavior found in earlier releases, where smooth() and noSmooth() could be used to enable and disable smoothing, though the quality was inferior.
* <br><br>hint(ENABLE_NATIVE_FONTS) - Use the native version fonts when they are installed, rather than the bitmapped version from a .vlw file. This is useful with the JAVA2D renderer setting, as it will improve font rendering speed. This is not enabled by default, because it can be misleading while testing because the type will look great on your machine (because you have the font installed) but lousy on others' machines if the identical font is unavailable. This option can only be set per-sketch, and must be called before any use of textFont().
* <br><br>hint(DISABLE_DEPTH_TEST) - Disable the zbuffer, allowing you to draw on top of everything at will. When depth testing is disabled, items will be drawn to the screen sequentially, like a painting. This hint is most often used to draw in 3D, then draw in 2D on top of it (for instance, to draw GUI controls in 2D on top of a 3D interface). Starting in release 0149, this will also clear the depth buffer. Restore the default with hint(ENABLE_DEPTH_TEST), but note that with the depth buffer cleared, any 3D drawing that happens later in draw() will ignore existing shapes on the screen.
* <br><br>hint(ENABLE_DEPTH_SORT) - Enable primitive z-sorting of triangles and lines in P3D and OPENGL. This can slow performance considerably, and the algorithm is not yet perfect. Restore the default with hint(DISABLE_DEPTH_SORT).
* <br><br>hint(DISABLE_OPENGL_ERROR_REPORT) - Speeds up the OPENGL renderer setting by not checking for errors while running. Undo with hint(ENABLE_OPENGL_ERROR_REPORT).
* <br><br><!--hint(ENABLE_ACCURATE_TEXTURES) - Enables better texture accuracy for the P3D renderer. This option will do a better job of dealing with textures in perspective. hint(DISABLE_ACCURATE_TEXTURES) returns to the default. This hint is not likely to last long.
* <br/> <br/>-->As of release 0149, unhint() has been removed in favor of adding additional ENABLE/DISABLE constants to reset the default behavior. This prevents the double negatives, and also reinforces which hints can be enabled or disabled.
*
* @webref rendering
* @param which name of the hint to be enabled or disabled
*
* @see processing.core.PGraphics
* @see processing.core.PApplet#createGraphics(int, int, String, String)
* @see processing.core.PApplet#size(int, int)
*/
public void hint(int which) {
if (which > 0) {
hints[which] = true;
} else {
hints[-which] = false;
}
}

public boolean hintEnabled(int which) {
if (which > 0) {
return hints[which];
} else {
return hints[-which];
}
}


//////////////////////////////////////////////////////////////

// VERTEX SHAPES

/**
* Start a new shape of type POLYGON
*/
public void beginShape() {
beginShape(POLYGON);
}


/**
* Start a new shape.
* <P>
* <B>Differences between beginShape() and line() and point() methods.</B>
* <P>
* beginShape() is intended to be more flexible at the expense of being
* a little more complicated to use. it handles more complicated shapes
* that can consist of many connected lines (so you get joins) or lines
* mixed with curves.
* <P>
* The line() and point() command are for the far more common cases
* (particularly for our audience) that simply need to draw a line
* or a point on the screen.
* <P>
* From the code side of things, line() may or may not call beginShape()
* to do the drawing. In the beta code, they do, but in the alpha code,
* they did not. they might be implemented one way or the other depending
* on tradeoffs of runtime efficiency vs. implementation efficiency &mdash
* meaning the speed that things run at vs. the speed it takes me to write
* the code and maintain it. for beta, the latter is most important so
* that's how things are implemented.
*/
public void beginShape(int kind) {
shape = kind;
}


/**
* Sets whether the upcoming vertex is part of an edge.
* Equivalent to glEdgeFlag(), for people familiar with OpenGL.
*/
public void edge(boolean edge) {
this.edge = edge;
}


/**
* Sets the current normal vector. Only applies with 3D rendering
* and inside a beginShape/endShape block.
* <P/>
* This is for drawing three dimensional shapes and surfaces,
* allowing you to specify a vector perpendicular to the surface
* of the shape, which determines how lighting affects it.
* <P/>
* For the most part, PGraphics3D will attempt to automatically
* assign normals to shapes, but since that's imperfect,
* this is a better option when you want more control.
* <P/>
* For people familiar with OpenGL, this function is basically
* identical to glNormal3f().
*/
public void normal(float nx, float ny, float nz) {
normalX = nx;
normalY = ny;
normalZ = nz;

// if drawing a shape and the normal hasn't been set yet,
// then we need to set the normals for each vertex so far
if (shape != 0) {
if (normalMode == NORMAL_MODE_AUTO) {
// either they set the normals, or they don't [0149]
// for (int i = vertex_start; i < vertexCount; i++) {
// vertices[i][NX] = normalX;
// vertices[i][NY] = normalY;
// vertices[i][NZ] = normalZ;
// }
// One normal per begin/end shape
normalMode = NORMAL_MODE_SHAPE;

} else if (normalMode == NORMAL_MODE_SHAPE) {
// a separate normal for each vertex
normalMode = NORMAL_MODE_VERTEX;
}
}
}


/**
* Set texture mode to either to use coordinates based on the IMAGE
* (more intuitive for new users) or NORMALIZED (better for advanced chaps)
*/
public void textureMode(int mode) {
this.textureMode = mode;
}


/**
* Set texture image for current shape.
* Needs to be called between @see beginShape and @see endShape
*
* @param image reference to a PImage object
*/
public void texture(PImage image) {
textureImage = image;
}


/**
* Removes texture image for current shape.
* Needs to be called between @see beginShape and @see endShape
*
*/
public void noTexture() {
textureImage = null;
}


protected void vertexCheck() {
if (vertexCount == vertices.length) {
float temp[][] = new float[vertexCount << 1][VERTEX_FIELD_COUNT];
System.arraycopy(vertices, 0, temp, 0, vertexCount);
vertices = temp;
}
}


public void vertex(float x, float y) {
vertexCheck();
float[] vertex = vertices[vertexCount];

curveVertexCount = 0;

vertex[X] = x;
vertex[Y] = y;

vertex[EDGE] = edge ? 1 : 0;

// if (fill) {
// vertex[R] = fillR;
// vertex[G] = fillG;
// vertex[B] = fillB;
// vertex[A] = fillA;
// }
boolean textured = textureImage != null;
if (fill || textured) {
if (textureImage == null) {
vertex[R] = fillR;
vertex[G] = fillG;
vertex[B] = fillB;
vertex[A] = fillA;
} else {
if (tint) {
vertex[R] = tintR;
vertex[G] = tintG;
vertex[B] = tintB;
vertex[A] = tintA;
} else {
vertex[R] = 1;
vertex[G] = 1;
vertex[B] = 1;
vertex[A] = 1;
}
}
}

if (stroke) {
vertex[SR] = strokeR;
vertex[SG] = strokeG;
vertex[SB] = strokeB;
vertex[SA] = strokeA;
vertex[SW] = strokeWeight;
}

if (textured) {
vertex[U] = textureU;
vertex[V] = textureV;
}

if (autoNormal) {
float norm2 = normalX * normalX + normalY * normalY + normalZ * normalZ;
if (norm2 < EPSILON) {
vertex[HAS_NORMAL] = 0;
} else {
if (Math.abs(norm2 - 1) > EPSILON) {
// The normal vector is not normalized.
float norm = PApplet.sqrt(norm2);
normalX /= norm;
normalY /= norm;
normalZ /= norm;
}
vertex[HAS_NORMAL] = 1;
}
} else {
vertex[HAS_NORMAL] = 1;
}

vertexCount++;
}


public void vertex(float x, float y, float z) {
vertexCheck();
float[] vertex = vertices[vertexCount];

// only do this if we're using an irregular (POLYGON) shape that
// will go through the triangulator. otherwise it'll do thinks like
// disappear in mathematically odd ways
// http://dev.processing.org/bugs/show_bug.cgi?id=444
if (shape == POLYGON) {
if (vertexCount > 0) {
float pvertex[] = vertices[vertexCount-1];
if ((Math.abs(pvertex[X] - x) < EPSILON) &&
(Math.abs(pvertex[Y] - y) < EPSILON) &&
(Math.abs(pvertex[Z] - z) < EPSILON)) {
// this vertex is identical, don't add it,
// because it will anger the triangulator
return;
}
}
}

// User called vertex(), so that invalidates anything queued up for curve
// vertices. If this is internally called by curveVertexSegment,
// then curveVertexCount will be saved and restored.
curveVertexCount = 0;

vertex[X] = x;
vertex[Y] = y;
vertex[Z] = z;

vertex[EDGE] = edge ? 1 : 0;

boolean textured = textureImage != null;
if (fill || textured) {
if (textureImage == null) {
vertex[R] = fillR;
vertex[G] = fillG;
vertex[B] = fillB;
vertex[A] = fillA;
} else {
if (tint) {
vertex[R] = tintR;
vertex[G] = tintG;
vertex[B] = tintB;
vertex[A] = tintA;
} else {
vertex[R] = 1;
vertex[G] = 1;
vertex[B] = 1;
vertex[A] = 1;
}
}

vertex[AR] = ambientR;
vertex[AG] = ambientG;
vertex[AB] = ambientB;

vertex[SPR] = specularR;
vertex[SPG] = specularG;
vertex[SPB] = specularB;
//vertex[SPA] = specularA;

vertex[SHINE] = shininess;

vertex[ER] = emissiveR;
vertex[EG] = emissiveG;
vertex[EB] = emissiveB;
}

if (stroke) {
vertex[SR] = strokeR;
vertex[SG] = strokeG;
vertex[SB] = strokeB;
vertex[SA] = strokeA;
vertex[SW] = strokeWeight;
}

if (textured) {
vertex[U] = textureU;
vertex[V] = textureV;
}

if (autoNormal) {
float norm2 = normalX * normalX + normalY * normalY + normalZ * normalZ;
if (norm2 < EPSILON) {
vertex[HAS_NORMAL] = 0;
} else {
if (Math.abs(norm2 - 1) > EPSILON) {
// The normal vector is not normalized.
float norm = PApplet.sqrt(norm2);
normalX /= norm;
normalY /= norm;
normalZ /= norm;
}
vertex[HAS_NORMAL] = 1;
}
} else {
vertex[HAS_NORMAL] = 1;
}

vertex[NX] = normalX;
vertex[NY] = normalY;
vertex[NZ] = normalZ;

vertex[BEEN_LIT] = 0;

vertexCount++;
}


/**
* Used by renderer subclasses or PShape to efficiently pass in already
* formatted vertex information.
* @param v vertex parameters, as a float array of length VERTEX_FIELD_COUNT
*/
public void vertex(float[] v) {
vertexCheck();
curveVertexCount = 0;
float[] vertex = vertices[vertexCount];
System.arraycopy(v, 0, vertex, 0, VERTEX_FIELD_COUNT);
vertexCount++;
}


public void vertex(float x, float y, float u, float v) {
vertexTexture(u, v);
vertex(x, y);
}


public void vertex(float x, float y, float z, float u, float v) {
vertexTexture(u, v);
vertex(x, y, z);
}


/**
* Internal method to copy all style information for the given vertex.
* Can be overridden by subclasses to handle only properties pertinent to
* that renderer. (e.g. no need to copy the emissive color in P2D)
*/
// protected void vertexStyle() {
// }


/**
* Set (U, V) coords for the next vertex in the current shape.
* This is ugly as its own function, and will (almost?) always be
* coincident with a call to vertex. As of beta, this was moved to
* the protected method you see here, and called from an optional
* param of and overloaded vertex().
* <p/>
* The parameters depend on the current textureMode. When using
* textureMode(IMAGE), the coordinates will be relative to the size
* of the image texture, when used with textureMode(NORMAL),
* they'll be in the range 0..1.
* <p/>
* Used by both PGraphics2D (for images) and PGraphics3D.
*/
protected void vertexTexture(float u, float v) {
if (textureImage == null) {
throw new RuntimeException("You must first call texture() before " +
"using u and v coordinates with vertex()");
}
if (textureMode == IMAGE) {
u /= (float) textureImage.width;
v /= (float) textureImage.height;
}

textureU = u;
textureV = v;

if (textureU < 0) textureU = 0;
else if (textureU > 1) textureU = 1;

if (textureV < 0) textureV = 0;
else if (textureV > 1) textureV = 1;
}


/** This feature is in testing, do not use or rely upon its implementation */
public void breakShape() {
showWarning("This renderer cannot currently handle concave shapes, " +
"or shapes with holes.");
}


public void endShape() {
endShape(OPEN);
}


public void endShape(int mode) {
}



//////////////////////////////////////////////////////////////

// CURVE/BEZIER VERTEX HANDLING


protected void bezierVertexCheck() {
if (shape == 0 || shape != POLYGON) {
throw new RuntimeException("beginShape() or beginShape(POLYGON) " +
"must be used before bezierVertex()");
}
if (vertexCount == 0) {
throw new RuntimeException("vertex() must be used at least once" +
"before bezierVertex()");
}
}


public void bezierVertex(float x2, float y2,
float x3, float y3,
float x4, float y4) {
bezierInitCheck();
bezierVertexCheck();
PMatrix3D draw = bezierDrawMatrix;

float[] prev = vertices[vertexCount-1];
float x1 = prev[X];
float y1 = prev[Y];

float xplot1 = draw.m10*x1 + draw.m11*x2 + draw.m12*x3 + draw.m13*x4;
float xplot2 = draw.m20*x1 + draw.m21*x2 + draw.m22*x3 + draw.m23*x4;
float xplot3 = draw.m30*x1 + draw.m31*x2 + draw.m32*x3 + draw.m33*x4;

float yplot1 = draw.m10*y1 + draw.m11*y2 + draw.m12*y3 + draw.m13*y4;
float yplot2 = draw.m20*y1 + draw.m21*y2 + draw.m22*y3 + draw.m23*y4;
float yplot3 = draw.m30*y1 + draw.m31*y2 + draw.m32*y3 + draw.m33*y4;

for (int j = 0; j < bezierDetail; j++) {
x1 += xplot1; xplot1 += xplot2; xplot2 += xplot3;
y1 += yplot1; yplot1 += yplot2; yplot2 += yplot3;
vertex(x1, y1);
}
}


public void bezierVertex(float x2, float y2, float z2,
float x3, float y3, float z3,
float x4, float y4, float z4) {
bezierInitCheck();
bezierVertexCheck();
PMatrix3D draw = bezierDrawMatrix;

float[] prev = vertices[vertexCount-1];
float x1 = prev[X];
float y1 = prev[Y];
float z1 = prev[Z];

float xplot1 = draw.m10*x1 + draw.m11*x2 + draw.m12*x3 + draw.m13*x4;
float xplot2 = draw.m20*x1 + draw.m21*x2 + draw.m22*x3 + draw.m23*x4;
float xplot3 = draw.m30*x1 + draw.m31*x2 + draw.m32*x3 + draw.m33*x4;

float yplot1 = draw.m10*y1 + draw.m11*y2 + draw.m12*y3 + draw.m13*y4;
float yplot2 = draw.m20*y1 + draw.m21*y2 + draw.m22*y3 + draw.m23*y4;
float yplot3 = draw.m30*y1 + draw.m31*y2 + draw.m32*y3 + draw.m33*y4;

float zplot1 = draw.m10*z1 + draw.m11*z2 + draw.m12*z3 + draw.m13*z4;
float zplot2 = draw.m20*z1 + draw.m21*z2 + draw.m22*z3 + draw.m23*z4;
float zplot3 = draw.m30*z1 + draw.m31*z2 + draw.m32*z3 + draw.m33*z4;

for (int j = 0; j < bezierDetail; j++) {
x1 += xplot1; xplot1 += xplot2; xplot2 += xplot3;
y1 += yplot1; yplot1 += yplot2; yplot2 += yplot3;
z1 += zplot1; zplot1 += zplot2; zplot2 += zplot3;
vertex(x1, y1, z1);
}
}


/**
* Perform initialization specific to curveVertex(), and handle standard
* error modes. Can be overridden by subclasses that need the flexibility.
*/
protected void curveVertexCheck() {
if (shape != POLYGON) {
throw new RuntimeException("You must use beginShape() or " +
"beginShape(POLYGON) before curveVertex()");
}
// to improve code init time, allocate on first use.
if (curveVertices == null) {
curveVertices = new float[128][3];
}

if (curveVertexCount == curveVertices.length) {
// Can't use PApplet.expand() cuz it doesn't do the copy properly
float[][] temp = new float[curveVertexCount << 1][3];
System.arraycopy(curveVertices, 0, temp, 0, curveVertexCount);
curveVertices = temp;
}
curveInitCheck();
}


public void curveVertex(float x, float y) {
curveVertexCheck();
float[] vertex = curveVertices[curveVertexCount];
vertex[X] = x;
vertex[Y] = y;
curveVertexCount++;

// draw a segment if there are enough points
if (curveVertexCount > 3) {
curveVertexSegment(curveVertices[curveVertexCount-4][X],
curveVertices[curveVertexCount-4][Y],
curveVertices[curveVertexCount-3][X],
curveVertices[curveVertexCount-3][Y],
curveVertices[curveVertexCount-2][X],
curveVertices[curveVertexCount-2][Y],
curveVertices[curveVertexCount-1][X],
curveVertices[curveVertexCount-1][Y]);
}
}


public void curveVertex(float x, float y, float z) {
curveVertexCheck();
float[] vertex = curveVertices[curveVertexCount];
vertex[X] = x;
vertex[Y] = y;
vertex[Z] = z;
curveVertexCount++;

// draw a segment if there are enough points
if (curveVertexCount > 3) {
curveVertexSegment(curveVertices[curveVertexCount-4][X],
curveVertices[curveVertexCount-4][Y],
curveVertices[curveVertexCount-4][Z],
curveVertices[curveVertexCount-3][X],
curveVertices[curveVertexCount-3][Y],
curveVertices[curveVertexCount-3][Z],
curveVertices[curveVertexCount-2][X],
curveVertices[curveVertexCount-2][Y],
curveVertices[curveVertexCount-2][Z],
curveVertices[curveVertexCount-1][X],
curveVertices[curveVertexCount-1][Y],
curveVertices[curveVertexCount-1][Z]);
}
}


/**
* Handle emitting a specific segment of Catmull-Rom curve. This can be
* overridden by subclasses that need more efficient rendering options.
*/
protected void curveVertexSegment(float x1, float y1,
float x2, float y2,
float x3, float y3,
float x4, float y4) {
float x0 = x2;
float y0 = y2;

PMatrix3D draw = curveDrawMatrix;

float xplot1 = draw.m10*x1 + draw.m11*x2 + draw.m12*x3 + draw.m13*x4;
float xplot2 = draw.m20*x1 + draw.m21*x2 + draw.m22*x3 + draw.m23*x4;
float xplot3 = draw.m30*x1 + draw.m31*x2 + draw.m32*x3 + draw.m33*x4;

float yplot1 = draw.m10*y1 + draw.m11*y2 + draw.m12*y3 + draw.m13*y4;
float yplot2 = draw.m20*y1 + draw.m21*y2 + draw.m22*y3 + draw.m23*y4;
float yplot3 = draw.m30*y1 + draw.m31*y2 + draw.m32*y3 + draw.m33*y4;

// vertex() will reset splineVertexCount, so save it
int savedCount = curveVertexCount;

vertex(x0, y0);
for (int j = 0; j < curveDetail; j++) {
x0 += xplot1; xplot1 += xplot2; xplot2 += xplot3;
y0 += yplot1; yplot1 += yplot2; yplot2 += yplot3;
vertex(x0, y0);
}
curveVertexCount = savedCount;
}


/**
* Handle emitting a specific segment of Catmull-Rom curve. This can be
* overridden by subclasses that need more efficient rendering options.
*/
protected void curveVertexSegment(float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3,
float x4, float y4, float z4) {
float x0 = x2;
float y0 = y2;
float z0 = z2;

PMatrix3D draw = curveDrawMatrix;

float xplot1 = draw.m10*x1 + draw.m11*x2 + draw.m12*x3 + draw.m13*x4;
float xplot2 = draw.m20*x1 + draw.m21*x2 + draw.m22*x3 + draw.m23*x4;
float xplot3 = draw.m30*x1 + draw.m31*x2 + draw.m32*x3 + draw.m33*x4;

float yplot1 = draw.m10*y1 + draw.m11*y2 + draw.m12*y3 + draw.m13*y4;
float yplot2 = draw.m20*y1 + draw.m21*y2 + draw.m22*y3 + draw.m23*y4;
float yplot3 = draw.m30*y1 + draw.m31*y2 + draw.m32*y3 + draw.m33*y4;

// vertex() will reset splineVertexCount, so save it
int savedCount = curveVertexCount;

float zplot1 = draw.m10*z1 + draw.m11*z2 + draw.m12*z3 + draw.m13*z4;
float zplot2 = draw.m20*z1 + draw.m21*z2 + draw.m22*z3 + draw.m23*z4;
float zplot3 = draw.m30*z1 + draw.m31*z2 + draw.m32*z3 + draw.m33*z4;

vertex(x0, y0, z0);
for (int j = 0; j < curveDetail; j++) {
x0 += xplot1; xplot1 += xplot2; xplot2 += xplot3;
y0 += yplot1; yplot1 += yplot2; yplot2 += yplot3;
z0 += zplot1; zplot1 += zplot2; zplot2 += zplot3;
vertex(x0, y0, z0);
}
curveVertexCount = savedCount;
}



//////////////////////////////////////////////////////////////

// SIMPLE SHAPES WITH ANALOGUES IN beginShape()


public void point(float x, float y) {
beginShape(POINTS);
vertex(x, y);
endShape();
}


/**
* Draws a point, a coordinate in space at the dimension of one pixel.
* The first parameter is the horizontal value for the point, the second
* value is the vertical value for the point, and the optional third value
* is the depth value. Drawing this shape in 3D using the <b>z</b>
* parameter requires the P3D or OPENGL parameter in combination with
* size as shown in the above example.
* <br><br>Due to what appears to be a bug in Apple's Java implementation,
* the point() and set() methods are extremely slow in some circumstances
* when used with the default renderer. Using P2D or P3D will fix the
* problem. Grouping many calls to point() or set() together can also
* help. (<a href="http://dev.processing.org/bugs/show_bug.cgi?id=1094">Bug 1094</a>)
*
* @webref shape:2d_primitives
* @param x x-coordinate of the point
* @param y y-coordinate of the point
* @param z z-coordinate of the point
*
* @see PGraphics#beginShape()
*/
public void point(float x, float y, float z) {
beginShape(POINTS);
vertex(x, y, z);
endShape();
}


public void line(float x1, float y1, float x2, float y2) {
beginShape(LINES);
vertex(x1, y1);
vertex(x2, y2);
endShape();
}



/**
* Draws a line (a direct path between two points) to the screen.
* The version of <b>line()</b> with four parameters draws the line in 2D.
* To color a line, use the <b>stroke()</b> function. A line cannot be
* filled, therefore the <b>fill()</b> method will not affect the color
* of a line. 2D lines are drawn with a width of one pixel by default,
* but this can be changed with the <b>strokeWeight()</b> function.
* The version with six parameters allows the line to be placed anywhere
* within XYZ space. Drawing this shape in 3D using the <b>z</b> parameter
* requires the P3D or OPENGL parameter in combination with size as shown
* in the above example.
*
* @webref shape:2d_primitives
* @param x1 x-coordinate of the first point
* @param y1 y-coordinate of the first point
* @param z1 z-coordinate of the first point
* @param x2 x-coordinate of the second point
* @param y2 y-coordinate of the second point
* @param z2 z-coordinate of the second point
*
* @see PGraphics#strokeWeight(float)
* @see PGraphics#strokeJoin(int)
* @see PGraphics#strokeCap(int)
* @see PGraphics#beginShape()
*/
public void line(float x1, float y1, float z1,
float x2, float y2, float z2) {
beginShape(LINES);
vertex(x1, y1, z1);
vertex(x2, y2, z2);
endShape();
}


/**
* A triangle is a plane created by connecting three points. The first two
* arguments specify the first point, the middle two arguments specify
* the second point, and the last two arguments specify the third point.
*
* @webref shape:2d_primitives
* @param x1 x-coordinate of the first point
* @param y1 y-coordinate of the first point
* @param x2 x-coordinate of the second point
* @param y2 y-coordinate of the second point
* @param x3 x-coordinate of the third point
* @param y3 y-coordinate of the third point
*
* @see PApplet#beginShape()
*/
public void triangle(float x1, float y1, float x2, float y2,
float x3, float y3) {
beginShape(TRIANGLES);
vertex(x1, y1);
vertex(x2, y2);
vertex(x3, y3);
endShape();
}


/**
* A quad is a quadrilateral, a four sided polygon. It is similar to
* a rectangle, but the angles between its edges are not constrained
* ninety degrees. The first pair of parameters (x1,y1) sets the
* first vertex and the subsequent pairs should proceed clockwise or
* counter-clockwise around the defined shape.
*
* @webref shape:2d_primitives
* @param x1 x-coordinate of the first corner
* @param y1 y-coordinate of the first corner
* @param x2 x-coordinate of the second corner
* @param y2 y-coordinate of the second corner
* @param x3 x-coordinate of the third corner
* @param y3 y-coordinate of the third corner
* @param x4 x-coordinate of the fourth corner
* @param y4 y-coordinate of the fourth corner
*
*/
public void quad(float x1, float y1, float x2, float y2,
float x3, float y3, float x4, float y4) {
beginShape(QUADS);
vertex(x1, y1);
vertex(x2, y2);
vertex(x3, y3);
vertex(x4, y4);
endShape();
}



//////////////////////////////////////////////////////////////

// RECT


public void rectMode(int mode) {
rectMode = mode;
}


/**
* Draws a rectangle to the screen. A rectangle is a four-sided shape with
* every angle at ninety degrees. The first two parameters set the location,
* the third sets the width, and the fourth sets the height. The origin is
* changed with the <b>rectMode()</b> function.
*
* @webref shape:2d_primitives
* @param a x-coordinate of the rectangle
* @param b y-coordinate of the rectangle
* @param c width of the rectangle
* @param d height of the rectangle
*
* @see PGraphics#rectMode(int)
* @see PGraphics#quad(float, float, float, float, float, float, float, float)
*/
public void rect(float a, float b, float c, float d) {
float hradius, vradius;
switch (rectMode) {
case CORNERS:
break;
case CORNER:
c += a; d += b;
break;
case RADIUS:
hradius = c;
vradius = d;
c = a + hradius;
d = b + vradius;
a -= hradius;
b -= vradius;
break;
case CENTER:
hradius = c / 2.0f;
vradius = d / 2.0f;
c = a + hradius;
d = b + vradius;
a -= hradius;
b -= vradius;
}

if (a > c) {
float temp = a; a = c; c = temp;
}

if (b > d) {
float temp = b; b = d; d = temp;
}

rectImpl(a, b, c, d);
}


protected void rectImpl(float x1, float y1, float x2, float y2) {
quad(x1, y1, x2, y1, x2, y2, x1, y2);
}


// Still need to do a lot of work here to make it behave across renderers
// (e.g. not all renderers use the vertices array)
// Also seems to be some issues on quality here (too dense)
// http://code.google.com/p/processing/issues/detail?id=265
private void quadraticVertex(float cpx, float cpy, float x, float y) {
float[] prev = vertices[vertexCount - 1];
float prevX = prev[X];
float prevY = prev[Y];
float cp1x = prevX + 2.0f/3.0f*(cpx - prevX);
float cp1y = prevY + 2.0f/3.0f*(cpy - prevY);
float cp2x = cp1x + (x - prevX)/3.0f;
float cp2y = cp1y + (y - prevY)/3.0f;
bezierVertex(cp1x, cp1y, cp2x, cp2y, x, y);
}


public void rect(float a, float b, float c, float d, float hr, float vr) {
float hradius, vradius;
switch (rectMode) {
case CORNERS:
break;
case CORNER:
c += a; d += b;
break;
case RADIUS:
hradius = c;
vradius = d;
c = a + hradius;
d = b + vradius;
a -= hradius;
b -= vradius;
break;
case CENTER:
hradius = c / 2.0f;
vradius = d / 2.0f;
c = a + hradius;
d = b + vradius;
a -= hradius;
b -= vradius;
}

if (a > c) {
float temp = a; a = c; c = temp;
}

if (b > d) {
float temp = b; b = d; d = temp;
}

rectImpl(a, b, c, d, hr, vr);
}


protected void rectImpl(float x1, float y1, float x2, float y2, float hr, float vr) {
beginShape();
// vertex(x1+hr, y1);
vertex(x2-hr, y1);
quadraticVertex(x2, y1, x2, y1+vr);
vertex(x2, y2-vr);
quadraticVertex(x2, y2, x2-hr, y2);
vertex(x1+hr, y2);
quadraticVertex(x1, y2, x1, y2-vr);
vertex(x1, y1+vr);
quadraticVertex(x1, y1, x1+hr, y1);
// endShape();
endShape(CLOSE);
}


public void rect(float a, float b, float c, float d,
float tl, float tr, float bl, float br) {
float hradius, vradius;
switch (rectMode) {
case CORNERS:
break;
case CORNER:
c += a; d += b;
break;
case RADIUS:
hradius = c;
vradius = d;
c = a + hradius;
d = b + vradius;
a -= hradius;
b -= vradius;
break;
case CENTER:
hradius = c / 2.0f;
vradius = d / 2.0f;
c = a + hradius;
d = b + vradius;
a -= hradius;
b -= vradius;
}

if (a > c) {
float temp = a; a = c; c = temp;
}

if (b > d) {
float temp = b; b = d; d = temp;
}

rectImpl(a, b, c, d, tl, tr, bl, br);
}


protected void rectImpl(float x1, float y1, float x2, float y2,
float tl, float tr, float bl, float br) {
beginShape();
// vertex(x1+tl, y1);
if (tr != 0) {
vertex(x2-tr, y1);
quadraticVertex(x2, y1, x2, y1+tr);
} else {
vertex(x2, y1);
}
if (br != 0) {
vertex(x2, y2-br);
quadraticVertex(x2, y2, x2-br, y2);
} else {
vertex(x2, y2);
}
if (bl != 0) {
vertex(x1+bl, y2);
quadraticVertex(x1, y2, x1, y2-bl);
} else {
vertex(x1, y2);
}
if (tl != 0) {
vertex(x1, y1+tl);
quadraticVertex(x1, y1, x1+tl, y1);
} else {
vertex(x1, y1);
}
// endShape();
endShape(CLOSE);
}



//////////////////////////////////////////////////////////////

// ELLIPSE AND ARC


/**
* The origin of the ellipse is modified by the <b>ellipseMode()</b>
* function. The default configuration is <b>ellipseMode(CENTER)</b>,
* which specifies the location of the ellipse as the center of the shape.
* The RADIUS mode is the same, but the width and height parameters to
* <b>ellipse()</b> specify the radius of the ellipse, rather than the
* diameter. The CORNER mode draws the shape from the upper-left corner
* of its bounding box. The CORNERS mode uses the four parameters to
* <b>ellipse()</b> to set two opposing corners of the ellipse's bounding
* box. The parameter must be written in "ALL CAPS" because Processing
* syntax is case sensitive.
*
* @webref shape:attributes
*
* @param mode Either CENTER, RADIUS, CORNER, or CORNERS.
* @see PApplet#ellipse(float, float, float, float)
*/
public void ellipseMode(int mode) {
ellipseMode = mode;
}


/**
* Draws an ellipse (oval) in the display window. An ellipse with an equal
* <b>width</b> and <b>height</b> is a circle. The first two parameters set
* the location, the third sets the width, and the fourth sets the height.
* The origin may be changed with the <b>ellipseMode()</b> function.
*
* @webref shape:2d_primitives
* @param a x-coordinate of the ellipse
* @param b y-coordinate of the ellipse
* @param c width of the ellipse
* @param d height of the ellipse
*
* @see PApplet#ellipseMode(int)
*/
public void ellipse(float a, float b, float c, float d) {
float x = a;
float y = b;
float w = c;
float h = d;

if (ellipseMode == CORNERS) {
w = c - a;
h = d - b;

} else if (ellipseMode == RADIUS) {
x = a - c;
y = b - d;
w = c * 2;
h = d * 2;

} else if (ellipseMode == DIAMETER) {
x = a - c/2f;
y = b - d/2f;
}

if (w < 0) { // undo negative width
x += w;
w = -w;
}

if (h < 0) { // undo negative height
y += h;
h = -h;
}

ellipseImpl(x, y, w, h);
}


protected void ellipseImpl(float x, float y, float w, float h) {
}


/**
* Draws an arc in the display window.
* Arcs are drawn along the outer edge of an ellipse defined by the
* <b>x</b>, <b>y</b>, <b>width</b> and <b>height</b> parameters.
* The origin or the arc's ellipse may be changed with the
* <b>ellipseMode()</b> function.
* The <b>start</b> and <b>stop</b> parameters specify the angles
* at which to draw the arc.
*
* @webref shape:2d_primitives
* @param a x-coordinate of the arc's ellipse
* @param b y-coordinate of the arc's ellipse
* @param c width of the arc's ellipse
* @param d height of the arc's ellipse
* @param start angle to start the arc, specified in radians
* @param stop angle to stop the arc, specified in radians
*
* @see PGraphics#ellipseMode(int)
* @see PGraphics#ellipse(float, float, float, float)
*/
public void arc(float a, float b, float c, float d,
float start, float stop) {
float x = a;
float y = b;
float w = c;
float h = d;

if (ellipseMode == CORNERS) {
w = c - a;
h = d - b;

} else if (ellipseMode == RADIUS) {
x = a - c;
y = b - d;
w = c * 2;
h = d * 2;

} else if (ellipseMode == CENTER) {
x = a - c/2f;
y = b - d/2f;
}

// make sure this loop will exit before starting while
if (Float.isInfinite(start) || Float.isInfinite(stop)) return;
// while (stop < start) stop += TWO_PI;
if (stop < start) return; // why bother

// make sure that we're starting at a useful point
while (start < 0) {
start += TWO_PI;
stop += TWO_PI;
}

if (stop - start > TWO_PI) {
start = 0;
stop = TWO_PI;
}

arcImpl(x, y, w, h, start, stop);
}


/**
* Start and stop are in radians, converted by the parent function.
* Note that the radians can be greater (or less) than TWO_PI.
* This is so that an arc can be drawn that crosses zero mark,
* and the user will still collect $200.
*/
protected void arcImpl(float x, float y, float w, float h,
float start, float stop) {
}



//////////////////////////////////////////////////////////////

// BOX


/**
* @param size dimension of the box in all dimensions, creates a cube
*/
public void box(float size) {
box(size, size, size);
}


/**
* A box is an extruded rectangle. A box with equal dimension
* on all sides is a cube.
*
* @webref shape:3d_primitives
* @param w dimension of the box in the x-dimension
* @param h dimension of the box in the y-dimension
* @param d dimension of the box in the z-dimension
*
* @see PApplet#sphere(float)
*/
public void box(float w, float h, float d) {
float x1 = -w/2f; float x2 = w/2f;
float y1 = -h/2f; float y2 = h/2f;
float z1 = -d/2f; float z2 = d/2f;

// TODO not the least bit efficient, it even redraws lines
// along the vertices. ugly ugly ugly!

beginShape(QUADS);

// front
normal(0, 0, 1);
vertex(x1, y1, z1);
vertex(x2, y1, z1);
vertex(x2, y2, z1);
vertex(x1, y2, z1);

// right
normal(1, 0, 0);
vertex(x2, y1, z1);
vertex(x2, y1, z2);
vertex(x2, y2, z2);
vertex(x2, y2, z1);

// back
normal(0, 0, -1);
vertex(x2, y1, z2);
vertex(x1, y1, z2);
vertex(x1, y2, z2);
vertex(x2, y2, z2);

// left
normal(-1, 0, 0);
vertex(x1, y1, z2);
vertex(x1, y1, z1);
vertex(x1, y2, z1);
vertex(x1, y2, z2);

// top
normal(0, 1, 0);
vertex(x1, y1, z2);
vertex(x2, y1, z2);
vertex(x2, y1, z1);
vertex(x1, y1, z1);

// bottom
normal(0, -1, 0);
vertex(x1, y2, z1);
vertex(x2, y2, z1);
vertex(x2, y2, z2);
vertex(x1, y2, z2);

endShape();
}



//////////////////////////////////////////////////////////////

// SPHERE


/**
* @param res number of segments (minimum 3) used per full circle revolution
*/
public void sphereDetail(int res) {
sphereDetail(res, res);
}


/**
* Controls the detail used to render a sphere by adjusting the number of
* vertices of the sphere mesh. The default resolution is 30, which creates
* a fairly detailed sphere definition with vertices every 360/30 = 12
* degrees. If you're going to render a great number of spheres per frame,
* it is advised to reduce the level of detail using this function.
* The setting stays active until <b>sphereDetail()</b> is called again with
* a new parameter and so should <i>not</i> be called prior to every
* <b>sphere()</b> statement, unless you wish to render spheres with
* different settings, e.g. using less detail for smaller spheres or ones
* further away from the camera. To control the detail of the horizontal
* and vertical resolution independently, use the version of the functions
* with two parameters.
*
* =advanced
* Code for sphereDetail() submitted by toxi [031031].
* Code for enhanced u/v version from davbol [080801].
*
* @webref shape:3d_primitives
* @param ures number of segments used horizontally (longitudinally)
* per full circle revolution
* @param vres number of segments used vertically (latitudinally)
* from top to bottom
*
* @see PGraphics#sphere(float)
*/
/**
* Set the detail level for approximating a sphere. The ures and vres params
* control the horizontal and vertical resolution.
*
*/
public void sphereDetail(int ures, int vres) {
if (ures < 3) ures = 3; // force a minimum res
if (vres < 2) vres = 2; // force a minimum res
if ((ures == sphereDetailU) && (vres == sphereDetailV)) return;

float delta = (float)SINCOS_LENGTH/ures;
float[] cx = new float[ures];
float[] cz = new float[ures];
// calc unit circle in XZ plane
for (int i = 0; i < ures; i++) {
cx[i] = cosLUT[(int) (i*delta) % SINCOS_LENGTH];
cz[i] = sinLUT[(int) (i*delta) % SINCOS_LENGTH];
}
// computing vertexlist
// vertexlist starts at south pole
int vertCount = ures * (vres-1) + 2;
int currVert = 0;

// re-init arrays to store vertices
sphereX = new float[vertCount];
sphereY = new float[vertCount];
sphereZ = new float[vertCount];

float angle_step = (SINCOS_LENGTH*0.5f)/vres;
float angle = angle_step;

// step along Y axis
for (int i = 1; i < vres; i++) {
float curradius = sinLUT[(int) angle % SINCOS_LENGTH];
float currY = -cosLUT[(int) angle % SINCOS_LENGTH];
for (int j = 0; j < ures; j++) {
sphereX[currVert] = cx[j] * curradius;
sphereY[currVert] = currY;
sphereZ[currVert++] = cz[j] * curradius;
}
angle += angle_step;
}
sphereDetailU = ures;
sphereDetailV = vres;
}


/**
* Draw a sphere with radius r centered at coordinate 0, 0, 0.
* A sphere is a hollow ball made from tessellated triangles.
* =advanced
* <P>
* Implementation notes:
* <P>
* cache all the points of the sphere in a static array
* top and bottom are just a bunch of triangles that land
* in the center point
* <P>
* sphere is a series of concentric circles who radii vary
* along the shape, based on, er.. cos or something
* <PRE>
* [toxi 031031] new sphere code. removed all multiplies with
* radius, as scale() will take care of that anyway
*
* [toxi 031223] updated sphere code (removed modulos)
* and introduced sphereAt(x,y,z,r)
* to avoid additional translate()'s on the user/sketch side
*
* [davbol 080801] now using separate sphereDetailU/V
* </PRE>
*
* @webref shape:3d_primitives
* @param r the radius of the sphere
*/
public void sphere(float r) {
if ((sphereDetailU < 3) || (sphereDetailV < 2)) {
sphereDetail(30);
}

edge(false);

// 1st ring from south pole
beginShape(TRIANGLE_STRIP);
for (int i = 0; i < sphereDetailU; i++) {
normal(0, -1, 0);
vertex(0, -r, 0);
normal(sphereX[i], sphereY[i], sphereZ[i]);
vertex(r * sphereX[i], r *sphereY[i], r * sphereZ[i]);
}
//normal(0, -1, 0);
vertex(0, -1, 0);
normal(sphereX[0], sphereY[0], sphereZ[0]);
vertex(r * sphereX[0], r * sphereY[0], r * sphereZ[0]);
endShape();

int v1,v11,v2;

// middle rings
int voff = 0;
for (int i = 2; i < sphereDetailV; i++) {
v1 = v11 = voff;
voff += sphereDetailU;
v2 = voff;
beginShape(TRIANGLE_STRIP);
for (int j = 0; j < sphereDetailU; j++) {
normal(sphereX[v1], sphereY[v1], sphereZ[v1]);
vertex(r * sphereX[v1], r * sphereY[v1], r * sphereZ[v1++]);
normal(sphereX[v2], sphereY[v2], sphereZ[v2]);
vertex(r * sphereX[v2], r * sphereY[v2], r * sphereZ[v2++]);
}
// close each ring
v1 = v11;
v2 = voff;
normal(sphereX[v1], sphereY[v1], sphereZ[v1]);
vertex(r * sphereX[v1], r * sphereY[v1], sphereZ[v1]);
normal(sphereX[v2], sphereY[v2], sphereZ[v2]);
vertex(r * sphereX[v2], r * sphereY[v2], r * sphereZ[v2]);
endShape();
}

// add the northern cap
beginShape(TRIANGLE_STRIP);
for (int i = 0; i < sphereDetailU; i++) {
v2 = voff + i;
normal(sphereX[v2], sphereY[v2], sphereZ[v2]);
vertex(r * sphereX[v2], r * sphereY[v2], r * sphereZ[v2]);
normal(0, 1, 0);
vertex(0, r, 0);
}
normal(sphereX[voff], sphereY[voff], sphereZ[voff]);
vertex(r * sphereX[voff], r * sphereY[voff], r * sphereZ[voff]);
normal(0, 1, 0);
vertex(0, r, 0);
endShape();

edge(true);
}



//////////////////////////////////////////////////////////////

// BEZIER

/**
* Evaluates the Bezier at point t for points a, b, c, d. The parameter t varies between 0 and 1, a and d are points on the curve, and b and c are the control points. This can be done once with the x coordinates and a second time with the y coordinates to get the location of a bezier curve at t.
*/

/**
* Evalutes quadratic bezier at point t for points a, b, c, d.
* The parameter t varies between 0 and 1. The a and d parameters are the
* on-curve points, b and c are the control points. To make a two-dimensional
* curve, call this function once with the x coordinates and a second time
* with the y coordinates to get the location of a bezier curve at t.
*
* =advanced
* For instance, to convert the following example:<PRE>
* stroke(255, 102, 0);
* line(85, 20, 10, 10);
* line(90, 90, 15, 80);
* stroke(0, 0, 0);
* bezier(85, 20, 10, 10, 90, 90, 15, 80);
*
* // draw it in gray, using 10 steps instead of the default 20
* // this is a slower way to do it, but useful if you need
* // to do things with the coordinates at each step
* stroke(128);
* beginShape(LINE_STRIP);
* for (int i = 0; i <= 10; i++) {
* float t = i / 10.0f;
* float x = bezierPoint(85, 10, 90, 15, t);
* float y = bezierPoint(20, 10, 90, 80, t);
* vertex(x, y);
* }
* endShape();</PRE>
*
* @webref shape:curves
* @param a coordinate of first point on the curve
* @param b coordinate of first control point
* @param c coordinate of second control point
* @param d coordinate of second point on the curve
* @param t value between 0 and 1
*
* @see PGraphics#bezier(float, float, float, float, float, float, float, float, float, float, float, float)
* @see PGraphics#bezierVertex(float, float, float, float, float, float)
* @see PGraphics#curvePoint(float, float, float, float, float)
*/
public float bezierPoint(float a, float b, float c, float d, float t) {
float t1 = 1.0f - t;
return a*t1*t1*t1 + 3*b*t*t1*t1 + 3*c*t*t*t1 + d*t*t*t;
}


/**
* Calculates the tangent of a point on a Bezier curve. There is a good
* definition of "tangent" at Wikipedia: <a href="http://en.wikipedia.org/wiki/Tangent" target="new">http://en.wikipedia.org/wiki/Tangent</a>
*
* =advanced
* Code submitted by Dave Bollinger (davol) for release 0136.
*
* @webref shape:curves
* @param a coordinate of first point on the curve
* @param b coordinate of first control point
* @param c coordinate of second control point
* @param d coordinate of second point on the curve
* @param t value between 0 and 1
*
* @see PGraphics#bezier(float, float, float, float, float, float, float, float, float, float, float, float)
* @see PGraphics#bezierVertex(float, float, float, float, float, float)
* @see PGraphics#curvePoint(float, float, float, float, float)
*/
public float bezierTangent(float a, float b, float c, float d, float t) {
return (3*t*t * (-a+3*b-3*c+d) +
6*t * (a-2*b+c) +
3 * (-a+b));
}


protected void bezierInitCheck() {
if (!bezierInited) {
bezierInit();
}
}


protected void bezierInit() {
// overkill to be broken out, but better parity with the curve stuff below
bezierDetail(bezierDetail);
bezierInited = true;
}


/**
* Sets the resolution at which Beziers display. The default value is 20. This function is only useful when using the P3D or OPENGL renderer as the default (JAVA2D) renderer does not use this information.
*
* @webref shape:curves
* @param detail resolution of the curves
*
* @see PApplet#curve(float, float, float, float, float, float, float, float, float, float, float, float)
* @see PApplet#curveVertex(float, float)
* @see PApplet#curveTightness(float)
*/
public void bezierDetail(int detail) {
bezierDetail = detail;

if (bezierDrawMatrix == null) {
bezierDrawMatrix = new PMatrix3D();
}

// setup matrix for forward differencing to speed up drawing
splineForward(detail, bezierDrawMatrix);

// multiply the basis and forward diff matrices together
// saves much time since this needn't be done for each curve
//mult_spline_matrix(bezierForwardMatrix, bezier_basis, bezierDrawMatrix, 4);
//bezierDrawMatrix.set(bezierForwardMatrix);
bezierDrawMatrix.apply(bezierBasisMatrix);
}


/**
* Draws a Bezier curve on the screen. These curves are defined by a series
* of anchor and control points. The first two parameters specify the first
* anchor point and the last two parameters specify the other anchor point.
* The middle parameters specify the control points which define the shape
* of the curve. Bezier curves were developed by French engineer Pierre
* Bezier. Using the 3D version of requires rendering with P3D or OPENGL
* (see the Environment reference for more information).
*
* =advanced
* Draw a cubic bezier curve. The first and last points are
* the on-curve points. The middle two are the 'control' points,
* or 'handles' in an application like Illustrator.
* <P>
* Identical to typing:
* <PRE>beginShape();
* vertex(x1, y1);
* bezierVertex(x2, y2, x3, y3, x4, y4);
* endShape();
* </PRE>
* In Postscript-speak, this would be:
* <PRE>moveto(x1, y1);
* curveto(x2, y2, x3, y3, x4, y4);</PRE>
* If you were to try and continue that curve like so:
* <PRE>curveto(x5, y5, x6, y6, x7, y7);</PRE>
* This would be done in processing by adding these statements:
* <PRE>bezierVertex(x5, y5, x6, y6, x7, y7)
* </PRE>
* To draw a quadratic (instead of cubic) curve,
* use the control point twice by doubling it:
* <PRE>bezier(x1, y1, cx, cy, cx, cy, x2, y2);</PRE>
*
* @webref shape:curves
* @param x1 coordinates for the first anchor point
* @param y1 coordinates for the first anchor point
* @param z1 coordinates for the first anchor point
* @param x2 coordinates for the first control point
* @param y2 coordinates for the first control point
* @param z2 coordinates for the first control point
* @param x3 coordinates for the second control point
* @param y3 coordinates for the second control point
* @param z3 coordinates for the second control point
* @param x4 coordinates for the second anchor point
* @param y4 coordinates for the second anchor point
* @param z4 coordinates for the second anchor point
*
* @see PGraphics#bezierVertex(float, float, float, float, float, float)
* @see PGraphics#curve(float, float, float, float, float, float, float, float, float, float, float, float)
*/
public void bezier(float x1, float y1,
float x2, float y2,
float x3, float y3,
float x4, float y4) {
beginShape();
vertex(x1, y1);
bezierVertex(x2, y2, x3, y3, x4, y4);
endShape();
}


public void bezier(float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3,
float x4, float y4, float z4) {
beginShape();
vertex(x1, y1, z1);
bezierVertex(x2, y2, z2,
x3, y3, z3,
x4, y4, z4);
endShape();
}



//////////////////////////////////////////////////////////////

// CATMULL-ROM CURVE


/**
* Evalutes the Catmull-Rom curve at point t for points a, b, c, d. The
* parameter t varies between 0 and 1, a and d are points on the curve,
* and b and c are the control points. This can be done once with the x
* coordinates and a second time with the y coordinates to get the
* location of a curve at t.
*
* @webref shape:curves
* @param a coordinate of first point on the curve
* @param b coordinate of second point on the curve
* @param c coordinate of third point on the curve
* @param d coordinate of fourth point on the curve
* @param t value between 0 and 1
*
* @see PGraphics#curve(float, float, float, float, float, float, float, float, float, float, float, float)
* @see PGraphics#curveVertex(float, float)
* @see PGraphics#bezierPoint(float, float, float, float, float)
*/
public float curvePoint(float a, float b, float c, float d, float t) {
curveInitCheck();

float tt = t * t;
float ttt = t * tt;
PMatrix3D cb = curveBasisMatrix;

// not optimized (and probably need not be)
return (a * (ttt*cb.m00 + tt*cb.m10 + t*cb.m20 + cb.m30) +
b * (ttt*cb.m01 + tt*cb.m11 + t*cb.m21 + cb.m31) +
c * (ttt*cb.m02 + tt*cb.m12 + t*cb.m22 + cb.m32) +
d * (ttt*cb.m03 + tt*cb.m13 + t*cb.m23 + cb.m33));
}


/**
* Calculates the tangent of a point on a Catmull-Rom curve. There is a good definition of "tangent" at Wikipedia: <a href="http://en.wikipedia.org/wiki/Tangent" target="new">http://en.wikipedia.org/wiki/Tangent</a>.
*
* =advanced
* Code thanks to Dave Bollinger (Bug #715)
*
* @webref shape:curves
* @param a coordinate of first point on the curve
* @param b coordinate of first control point
* @param c coordinate of second control point
* @param d coordinate of second point on the curve
* @param t value between 0 and 1
*
* @see PGraphics#curve(float, float, float, float, float, float, float, float, float, float, float, float)
* @see PGraphics#curveVertex(float, float)
* @see PGraphics#curvePoint(float, float, float, float, float)
* @see PGraphics#bezierTangent(float, float, float, float, float)
*/
public float curveTangent(float a, float b, float c, float d, float t) {
curveInitCheck();

float tt3 = t * t * 3;
float t2 = t * 2;
PMatrix3D cb = curveBasisMatrix;

// not optimized (and probably need not be)
return (a * (tt3*cb.m00 + t2*cb.m10 + cb.m20) +
b * (tt3*cb.m01 + t2*cb.m11 + cb.m21) +
c * (tt3*cb.m02 + t2*cb.m12 + cb.m22) +
d * (tt3*cb.m03 + t2*cb.m13 + cb.m23) );
}


/**
* Sets the resolution at which curves display. The default value is 20.
* This function is only useful when using the P3D or OPENGL renderer as
* the default (JAVA2D) renderer does not use this information.
*
* @webref shape:curves
* @param detail resolution of the curves
*
* @see PGraphics#curve(float, float, float, float, float, float, float, float, float, float, float, float)
* @see PGraphics#curveVertex(float, float)
* @see PGraphics#curveTightness(float)
*/
public void curveDetail(int detail) {
curveDetail = detail;
curveInit();
}


/**
* Modifies the quality of forms created with <b>curve()</b> and
*<b>curveVertex()</b>. The parameter <b>squishy</b> determines how the
* curve fits to the vertex points. The value 0.0 is the default value for
* <b>squishy</b> (this value defines the curves to be Catmull-Rom splines)
* and the value 1.0 connects all the points with straight lines.
* Values within the range -5.0 and 5.0 will deform the curves but
* will leave them recognizable and as values increase in magnitude,
* they will continue to deform.
*
* @webref shape:curves
* @param tightness amount of deformation from the original vertices
*
* @see PGraphics#curve(float, float, float, float, float, float, float, float, float, float, float, float)
* @see PGraphics#curveVertex(float, float)
*
*/
public void curveTightness(float tightness) {
curveTightness = tightness;
curveInit();
}


protected void curveInitCheck() {
if (!curveInited) {
curveInit();
}
}


/**
* Set the number of segments to use when drawing a Catmull-Rom
* curve, and setting the s parameter, which defines how tightly
* the curve fits to each vertex. Catmull-Rom curves are actually
* a subset of this curve type where the s is set to zero.
* <P>
* (This function is not optimized, since it's not expected to
* be called all that often. there are many juicy and obvious
* opimizations in here, but it's probably better to keep the
* code more readable)
*/
protected void curveInit() {
// allocate only if/when used to save startup time
if (curveDrawMatrix == null) {
curveBasisMatrix = new PMatrix3D();
curveDrawMatrix = new PMatrix3D();
curveInited = true;
}

float s = curveTightness;
curveBasisMatrix.set((s-1)/2f, (s+3)/2f, (-3-s)/2f, (1-s)/2f,
(1-s), (-5-s)/2f, (s+2), (s-1)/2f,
(s-1)/2f, 0, (1-s)/2f, 0,
0, 1, 0, 0);

//setup_spline_forward(segments, curveForwardMatrix);
splineForward(curveDetail, curveDrawMatrix);

if (bezierBasisInverse == null) {
bezierBasisInverse = bezierBasisMatrix.get();
bezierBasisInverse.invert();
curveToBezierMatrix = new PMatrix3D();
}

// TODO only needed for PGraphicsJava2D? if so, move it there
// actually, it's generally useful for other renderers, so keep it
// or hide the implementation elsewhere.
curveToBezierMatrix.set(curveBasisMatrix);
curveToBezierMatrix.preApply(bezierBasisInverse);

// multiply the basis and forward diff matrices together
// saves much time since this needn't be done for each curve
curveDrawMatrix.apply(curveBasisMatrix);
}


/**
* Draws a curved line on the screen. The first and second parameters
* specify the beginning control point and the last two parameters specify
* the ending control point. The middle parameters specify the start and
* stop of the curve. Longer curves can be created by putting a series of
* <b>curve()</b> functions together or using <b>curveVertex()</b>.
* An additional function called <b>curveTightness()</b> provides control
* for the visual quality of the curve. The <b>curve()</b> function is an
* implementation of Catmull-Rom splines. Using the 3D version of requires
* rendering with P3D or OPENGL (see the Environment reference for more
* information).
*
* =advanced
* As of revision 0070, this function no longer doubles the first
* and last points. The curves are a bit more boring, but it's more
* mathematically correct, and properly mirrored in curvePoint().
* <P>
* Identical to typing out:<PRE>
* beginShape();
* curveVertex(x1, y1);
* curveVertex(x2, y2);
* curveVertex(x3, y3);
* curveVertex(x4, y4);
* endShape();
* </PRE>
*
* @webref shape:curves
* @param x1 coordinates for the beginning control point
* @param y1 coordinates for the beginning control point
* @param z1 coordinates for the beginning control point
* @param x2 coordinates for the first point
* @param y2 coordinates for the first point
* @param z2 coordinates for the first point
* @param x3 coordinates for the second point
* @param y3 coordinates for the second point
* @param z3 coordinates for the second point
* @param x4 coordinates for the ending control point
* @param y4 coordinates for the ending control point
* @param z4 coordinates for the ending control point
*
* @see PGraphics#curveVertex(float, float)
* @see PGraphics#curveTightness(float)
* @see PGraphics#bezier(float, float, float, float, float, float, float, float, float, float, float, float)
*/
public void curve(float x1, float y1,
float x2, float y2,
float x3, float y3,
float x4, float y4) {
beginShape();
curveVertex(x1, y1);
curveVertex(x2, y2);
curveVertex(x3, y3);
curveVertex(x4, y4);
endShape();
}


public void curve(float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3,
float x4, float y4, float z4) {
beginShape();
curveVertex(x1, y1, z1);
curveVertex(x2, y2, z2);
curveVertex(x3, y3, z3);
curveVertex(x4, y4, z4);
endShape();
}



//////////////////////////////////////////////////////////////

// SPLINE UTILITY FUNCTIONS (used by both Bezier and Catmull-Rom)


/**
* Setup forward-differencing matrix to be used for speedy
* curve rendering. It's based on using a specific number
* of curve segments and just doing incremental adds for each
* vertex of the segment, rather than running the mathematically
* expensive cubic equation.
* @param segments number of curve segments to use when drawing
* @param matrix target object for the new matrix
*/
protected void splineForward(int segments, PMatrix3D matrix) {
float f = 1.0f / segments;
float ff = f * f;
float fff = ff * f;

matrix.set(0, 0, 0, 1,
fff, ff, f, 0,
6*fff, 2*ff, 0, 0,
6*fff, 0, 0, 0);
}



//////////////////////////////////////////////////////////////

// SMOOTHING


/**
* If true in PImage, use bilinear interpolation for copy()
* operations. When inherited by PGraphics, also controls shapes.
*/
public void smooth() {
smooth = true;
}


/**
* Disable smoothing. See smooth().
*/
public void noSmooth() {
smooth = false;
}



//////////////////////////////////////////////////////////////

// IMAGE


/**
* Modifies the location from which images draw. The default mode is
* <b>imageMode(CORNER)</b>, which specifies the location to be the
* upper-left corner and uses the fourth and fifth parameters of
* <b>image()</b> to set the image's width and height. The syntax
* <b>imageMode(CORNERS)</b> uses the second and third parameters of
* <b>image()</b> to set the location of one corner of the image and
* uses the fourth and fifth parameters to set the opposite corner.
* Use <b>imageMode(CENTER)</b> to draw images centered at the given
* x and y position.
* <br><br>The parameter to <b>imageMode()</b> must be written in
* ALL CAPS because Processing syntax is case sensitive.
*
* @webref image:loading_displaying
* @param mode Either CORNER, CORNERS, or CENTER
*
* @see processing.core.PApplet#loadImage(String, String)
* @see processing.core.PImage
* @see processing.core.PApplet#image(PImage, float, float, float, float)
* @see processing.core.PGraphics#background(float, float, float, float)
*/
public void imageMode(int mode) {
if ((mode == CORNER) || (mode == CORNERS) || (mode == CENTER)) {
imageMode = mode;
} else {
String msg =
"imageMode() only works with CORNER, CORNERS, or CENTER";
throw new RuntimeException(msg);
}
}


public void image(PImage image, float x, float y) {
// Starting in release 0144, image errors are simply ignored.
// loadImageAsync() sets width and height to -1 when loading fails.
if (image.width == -1 || image.height == -1) return;

if (imageMode == CORNER || imageMode == CORNERS) {
imageImpl(image,
x, y, x+image.width, y+image.height,
0, 0, image.width, image.height);

} else if (imageMode == CENTER) {
float x1 = x - image.width/2;
float y1 = y - image.height/2;
imageImpl(image,
x1, y1, x1+image.width, y1+image.height,
0, 0, image.width, image.height);
}
}


/**
* Displays images to the screen. The images must be in the sketch's "data"
* directory to load correctly. Select "Add file..." from the "Sketch" menu
* to add the image. Processing currently works with GIF, JPEG, and Targa
* images. The color of an image may be modified with the <b>tint()</b>
* function and if a GIF has transparency, it will maintain its transparency.
* The <b>img</b> parameter specifies the image to display and the <b>x</b>
* and <b>y</b> parameters define the location of the image from its
* upper-left corner. The image is displayed at its original size unless
* the <b>width</b> and <b>height</b> parameters specify a different size.
* The <b>imageMode()</b> function changes the way the parameters work.
* A call to <b>imageMode(CORNERS)</b> will change the width and height
* parameters to define the x and y values of the opposite corner of the
* image.
*
* =advanced
* Starting with release 0124, when using the default (JAVA2D) renderer,
* smooth() will also improve image quality of resized images.
*
* @webref image:loading_displaying
* @param image the image to display
* @param x x-coordinate of the image
* @param y y-coordinate of the image
* @param c width to display the image
* @param d height to display the image
*
* @see processing.core.PApplet#loadImage(String, String)
* @see processing.core.PImage
* @see processing.core.PGraphics#imageMode(int)
* @see processing.core.PGraphics#tint(float)
* @see processing.core.PGraphics#background(float, float, float, float)
* @see processing.core.PGraphics#alpha(int)
*/
public void image(PImage image, float x, float y, float c, float d) {
image(image, x, y, c, d, 0, 0, image.width, image.height);
}


/**
* Draw an image(), also specifying u/v coordinates.
* In this method, the u, v coordinates are always based on image space
* location, regardless of the current textureMode().
*/
public void image(PImage image,
float a, float b, float c, float d,
int u1, int v1, int u2, int v2) {
// Starting in release 0144, image errors are simply ignored.
// loadImageAsync() sets width and height to -1 when loading fails.
if (image.width == -1 || image.height == -1) return;

if (imageMode == CORNER) {
if (c < 0) { // reset a negative width
a += c; c = -c;
}
if (d < 0) { // reset a negative height
b += d; d = -d;
}

imageImpl(image,
a, b, a + c, b + d,
u1, v1, u2, v2);

} else if (imageMode == CORNERS) {
if (c < a) { // reverse because x2 < x1
float temp = a; a = c; c = temp;
}
if (d < b) { // reverse because y2 < y1
float temp = b; b = d; d = temp;
}

imageImpl(image,
a, b, c, d,
u1, v1, u2, v2);

} else if (imageMode == CENTER) {
// c and d are width/height
if (c < 0) c = -c;
if (d < 0) d = -d;
float x1 = a - c/2;
float y1 = b - d/2;

imageImpl(image,
x1, y1, x1 + c, y1 + d,
u1, v1, u2, v2);
}
}


/**
* Expects x1, y1, x2, y2 coordinates where (x2 >= x1) and (y2 >= y1).
* If tint() has been called, the image will be colored.
* <p/>
* The default implementation draws an image as a textured quad.
* The (u, v) coordinates are in image space (they're ints, after all..)
*/
protected void imageImpl(PImage image,
float x1, float y1, float x2, float y2,
int u1, int v1, int u2, int v2) {
boolean savedStroke = stroke;
// boolean savedFill = fill;
int savedTextureMode = textureMode;

stroke = false;
// fill = true;
textureMode = IMAGE;

// float savedFillR = fillR;
// float savedFillG = fillG;
// float savedFillB = fillB;
// float savedFillA = fillA;
//
// if (tint) {
// fillR = tintR;
// fillG = tintG;
// fillB = tintB;
// fillA = tintA;
//
// } else {
// fillR = 1;
// fillG = 1;
// fillB = 1;
// fillA = 1;
// }

beginShape(QUADS);
texture(image);
vertex(x1, y1, u1, v1);
vertex(x1, y2, u1, v2);
vertex(x2, y2, u2, v2);
vertex(x2, y1, u2, v1);
endShape();

stroke = savedStroke;
// fill = savedFill;
textureMode = savedTextureMode;

// fillR = savedFillR;
// fillG = savedFillG;
// fillB = savedFillB;
// fillA = savedFillA;
}



//////////////////////////////////////////////////////////////

// SHAPE


/**
* Modifies the location from which shapes draw.
* The default mode is <b>shapeMode(CORNER)</b>, which specifies the
* location to be the upper left corner of the shape and uses the third
* and fourth parameters of <b>shape()</b> to specify the width and height.
* The syntax <b>shapeMode(CORNERS)</b> uses the first and second parameters
* of <b>shape()</b> to set the location of one corner and uses the third
* and fourth parameters to set the opposite corner.
* The syntax <b>shapeMode(CENTER)</b> draws the shape from its center point
* and uses the third and forth parameters of <b>shape()</b> to specify the
* width and height.
* The parameter must be written in "ALL CAPS" because Processing syntax
* is case sensitive.
*
* @param mode One of CORNER, CORNERS, CENTER
*
* @webref shape:loading_displaying
* @see PGraphics#shape(PShape)
* @see PGraphics#rectMode(int)
*/
public void shapeMode(int mode) {
this.shapeMode = mode;
}


public void shape(PShape shape) {
if (shape.isVisible()) { // don't do expensive matrix ops if invisible
if (shapeMode == CENTER) {
pushMatrix();
translate(-shape.getWidth()/2, -shape.getHeight()/2);
}

shape.draw(this); // needs to handle recorder too

if (shapeMode == CENTER) {
popMatrix();
}
}
}


/**
* Convenience method to draw at a particular location.
*/
public void shape(PShape shape, float x, float y) {
if (shape.isVisible()) { // don't do expensive matrix ops if invisible
pushMatrix();

if (shapeMode == CENTER) {
translate(x - shape.getWidth()/2, y - shape.getHeight()/2);

} else if ((shapeMode == CORNER) || (shapeMode == CORNERS)) {
translate(x, y);
}
shape.draw(this);

popMatrix();
}
}


/**
* Displays shapes to the screen. The shapes must be in the sketch's "data"
* directory to load correctly. Select "Add file..." from the "Sketch" menu
* to add the shape.
* Processing currently works with SVG shapes only.
* The <b>sh</b> parameter specifies the shape to display and the <b>x</b>
* and <b>y</b> parameters define the location of the shape from its
* upper-left corner.
* The shape is displayed at its original size unless the <b>width</b>
* and <b>height</b> parameters specify a different size.
* The <b>shapeMode()</b> function changes the way the parameters work.
* A call to <b>shapeMode(CORNERS)</b>, for example, will change the width
* and height parameters to define the x and y values of the opposite corner
* of the shape.
* <br><br>
* Note complex shapes may draw awkwardly with P2D, P3D, and OPENGL. Those
* renderers do not yet support shapes that have holes or complicated breaks.
*
* @param shape
* @param x x-coordinate of the shape
* @param y y-coordinate of the shape
* @param c width to display the shape
* @param d height to display the shape
*
* @webref shape:loading_displaying
* @see PShape
* @see PGraphics#loadShape(String)
* @see PGraphics#shapeMode(int)
*/
public void shape(PShape shape, float x, float y, float c, float d) {
if (shape.isVisible()) { // don't do expensive matrix ops if invisible
pushMatrix();

if (shapeMode == CENTER) {
// x and y are center, c and d refer to a diameter
translate(x - c/2f, y - d/2f);
scale(c / shape.getWidth(), d / shape.getHeight());

} else if (shapeMode == CORNER) {
translate(x, y);
scale(c / shape.getWidth(), d / shape.getHeight());

} else if (shapeMode == CORNERS) {
// c and d are x2/y2, make them into width/height
c -= x;
d -= y;
// then same as above
translate(x, y);
scale(c / shape.getWidth(), d / shape.getHeight());
}
shape.draw(this);

popMatrix();
}
}



//////////////////////////////////////////////////////////////

// TEXT/FONTS


/**
* Sets the alignment of the text to one of LEFT, CENTER, or RIGHT.
* This will also reset the vertical text alignment to BASELINE.
*/
public void textAlign(int align) {
textAlign(align, BASELINE);
}


/**
* Sets the horizontal and vertical alignment of the text. The horizontal
* alignment can be one of LEFT, CENTER, or RIGHT. The vertical alignment
* can be TOP, BOTTOM, CENTER, or the BASELINE (the default).
*/
public void textAlign(int alignX, int alignY) {
textAlign = alignX;
textAlignY = alignY;
}


/**
* Returns the ascent of the current font at the current size.
* This is a method, rather than a variable inside the PGraphics object
* because it requires calculation.
*/
public float textAscent() {
if (textFont == null) {
defaultFontOrDeath("textAscent");
}
return textFont.ascent() * ((textMode == SCREEN) ? textFont.size : textSize);
}


/**
* Returns the descent of the current font at the current size.
* This is a method, rather than a variable inside the PGraphics object
* because it requires calculation.
*/
public float textDescent() {
if (textFont == null) {
defaultFontOrDeath("textDescent");
}
return textFont.descent() * ((textMode == SCREEN) ? textFont.size : textSize);
}


/**
* Sets the current font. The font's size will be the "natural"
* size of this font (the size that was set when using "Create Font").
* The leading will also be reset.
*/
public void textFont(PFont which) {
if (which != null) {
textFont = which;
if (hints[ENABLE_NATIVE_FONTS]) {
//if (which.font == null) {
which.findFont();
//}
}
/*
textFontNative = which.font;

//textFontNativeMetrics = null;
// changed for rev 0104 for textMode(SHAPE) in opengl
if (textFontNative != null) {
// TODO need a better way to handle this. could use reflection to get
// rid of the warning, but that'd be a little silly. supporting this is
// an artifact of supporting java 1.1, otherwise we'd use getLineMetrics,
// as recommended by the @deprecated flag.
textFontNativeMetrics =
Toolkit.getDefaultToolkit().getFontMetrics(textFontNative);
// The following is what needs to be done, however we need to be able
// to get the actual graphics context where the drawing is happening.
// For instance, parent.getGraphics() doesn't work for OpenGL since
// an OpenGL drawing surface is an embedded component.
// if (parent != null) {
// textFontNativeMetrics = parent.getGraphics().getFontMetrics(textFontNative);
// }

// float w = font.getStringBounds(text, g2.getFontRenderContext()).getWidth();
}
*/
textSize(which.size);

} else {
throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT);
}
}


/**
* Useful function to set the font and size at the same time.
*/
public void textFont(PFont which, float size) {
textFont(which);
textSize(size);
}


/**
* Set the text leading to a specific value. If using a custom
* value for the text leading, you'll have to call textLeading()
* again after any calls to textSize().
*/
public void textLeading(float leading) {
textLeading = leading;
}


/**
* Sets the text rendering/placement to be either SCREEN (direct
* to the screen, exact coordinates, only use the font's original size)
* or MODEL (the default, where text is manipulated by translate() and
* can have a textSize). The text size cannot be set when using
* textMode(SCREEN), because it uses the pixels directly from the font.
*/
public void textMode(int mode) {
// CENTER and MODEL overlap (they're both 3)
if ((mode == LEFT) || (mode == RIGHT)) {
showWarning("Since Processing beta, textMode() is now textAlign().");
return;
}
// if ((mode != SCREEN) && (mode != MODEL)) {
// showError("Only textMode(SCREEN) and textMode(MODEL) " +
// "are available with this renderer.");
// }

if (textModeCheck(mode)) {
textMode = mode;
} else {
String modeStr = String.valueOf(mode);
switch (mode) {
case SCREEN: modeStr = "SCREEN"; break;
case MODEL: modeStr = "MODEL"; break;
case SHAPE: modeStr = "SHAPE"; break;
}
showWarning("textMode(" + modeStr + ") is not supported by this renderer.");
}

// reset the font to its natural size
// (helps with width calculations and all that)
//if (textMode == SCREEN) {
//textSize(textFont.size);
//}

//} else {
//throw new RuntimeException("use textFont() before textMode()");
//}
}


protected boolean textModeCheck(int mode) {
return true;
}


/**
* Sets the text size, also resets the value for the leading.
*/
public void textSize(float size) {
if (textFont == null) {
defaultFontOrDeath("textSize", size);
}
textSize = size;
textLeading = (textAscent() + textDescent()) * 1.275f;
}

// ........................................................


public float textWidth(char c) {
textWidthBuffer[0] = c;
return textWidthImpl(textWidthBuffer, 0, 1);
}


/**
* Return the width of a line of text. If the text has multiple
* lines, this returns the length of the longest line.
*/
public float textWidth(String str) {
if (textFont == null) {
defaultFontOrDeath("textWidth");
}

int length = str.length();
if (length > textWidthBuffer.length) {
textWidthBuffer = new char[length + 10];
}
str.getChars(0, length, textWidthBuffer, 0);

float wide = 0;
int index = 0;
int start = 0;

while (index < length) {
if (textWidthBuffer[index] == '\n') {
wide = Math.max(wide, textWidthImpl(textWidthBuffer, start, index));
start = index+1;
}
index++;
}
if (start < length) {
wide = Math.max(wide, textWidthImpl(textWidthBuffer, start, index));
}
return wide;
}


/**
* TODO not sure if this stays...
*/
public float textWidth(char[] chars, int start, int length) {
return textWidthImpl(chars, start, start + length);
}


/**
* Implementation of returning the text width of
* the chars [start, stop) in the buffer.
* Unlike the previous version that was inside PFont, this will
* return the size not of a 1 pixel font, but the actual current size.
*/
protected float textWidthImpl(char buffer[], int start, int stop) {
float wide = 0;
for (int i = start; i < stop; i++) {
// could add kerning here, but it just ain't implemented
wide += textFont.width(buffer[i]) * textSize;
}
return wide;
}


// ........................................................


/**
* Write text where we just left off.
*/
public void text(char c) {
text(c, textX, textY, textZ);
}


/**
* Draw a single character on screen.
* Extremely slow when used with textMode(SCREEN) and Java 2D,
* because loadPixels has to be called first and updatePixels last.
*/
public void text(char c, float x, float y) {
if (textFont == null) {
defaultFontOrDeath("text");
}

if (textMode == SCREEN) loadPixels();

if (textAlignY == CENTER) {
y += textAscent() / 2;
} else if (textAlignY == TOP) {
y += textAscent();
} else if (textAlignY == BOTTOM) {
y -= textDescent();
//} else if (textAlignY == BASELINE) {
// do nothing
}

textBuffer[0] = c;
textLineAlignImpl(textBuffer, 0, 1, x, y);

if (textMode == SCREEN) updatePixels();
}


/**
* Draw a single character on screen (with a z coordinate)
*/
public void text(char c, float x, float y, float z) {
// if ((z != 0) && (textMode == SCREEN)) {
// String msg = "textMode(SCREEN) cannot have a z coordinate";
// throw new RuntimeException(msg);
// }

if (z != 0) translate(0, 0, z); // slowness, badness

text(c, x, y);
textZ = z;

if (z != 0) translate(0, 0, -z);
}


/**
* Write text where we just left off.
*/
public void text(String str) {
text(str, textX, textY, textZ);
}


/**
* Draw a chunk of text.
* Newlines that are \n (Unix newline or linefeed char, ascii 10)
* are honored, but \r (carriage return, Windows and Mac OS) are
* ignored.
*/
public void text(String str, float x, float y) {
if (textFont == null) {
defaultFontOrDeath("text");
}

if (textMode == SCREEN) loadPixels();

int length = str.length();
if (length > textBuffer.length) {
textBuffer = new char[length + 10];
}
str.getChars(0, length, textBuffer, 0);
text(textBuffer, 0, length, x, y);
}


/**
* Method to draw text from an array of chars. This method will usually be
* more efficient than drawing from a String object, because the String will
* not be converted to a char array before drawing.
*/
public void text(char[] chars, int start, int stop, float x, float y) {
// If multiple lines, sum the height of the additional lines
float high = 0; //-textAscent();
for (int i = start; i < stop; i++) {
if (chars[i] == '\n') {
high += textLeading;
}
}
if (textAlignY == CENTER) {
// for a single line, this adds half the textAscent to y
// for multiple lines, subtract half the additional height
//y += (textAscent() - textDescent() - high)/2;
y += (textAscent() - high)/2;
} else if (textAlignY == TOP) {
// for a single line, need to add textAscent to y
// for multiple lines, no different
y += textAscent();
} else if (textAlignY == BOTTOM) {
// for a single line, this is just offset by the descent
// for multiple lines, subtract leading for each line
y -= textDescent() + high;
//} else if (textAlignY == BASELINE) {
// do nothing
}

// int start = 0;
int index = 0;
while (index < stop) { //length) {
if (chars[index] == '\n') {
textLineAlignImpl(chars, start, index, x, y);
start = index + 1;
y += textLeading;
}
index++;
}
if (start < stop) { //length) {
textLineAlignImpl(chars, start, index, x, y);
}
if (textMode == SCREEN) updatePixels();
}


/**
* Same as above but with a z coordinate.
*/
public void text(String str, float x, float y, float z) {
if (z != 0) translate(0, 0, z); // slow!

text(str, x, y);
textZ = z;

if (z != 0) translate(0, 0, -z); // inaccurate!
}


public void text(char[] chars, int start, int stop,
float x, float y, float z) {
if (z != 0) translate(0, 0, z); // slow!

text(chars, start, stop, x, y);
textZ = z;

if (z != 0) translate(0, 0, -z); // inaccurate!
}


/**
* Draw text in a box that is constrained to a particular size.
* The current rectMode() determines what the coordinates mean
* (whether x1/y1/x2/y2 or x/y/w/h).
* <P/>
* Note that the x,y coords of the start of the box
* will align with the *ascent* of the text, not the baseline,
* as is the case for the other text() functions.
* <P/>
* Newlines that are \n (Unix newline or linefeed char, ascii 10)
* are honored, and \r (carriage return, Windows and Mac OS) are
* ignored.
*/
public void text(String str, float x1, float y1, float x2, float y2) {
if (textFont == null) {
defaultFontOrDeath("text");
}

if (textMode == SCREEN) loadPixels();

float hradius, vradius;
switch (rectMode) {
case CORNER:
x2 += x1; y2 += y1;
break;
case RADIUS:
hradius = x2;
vradius = y2;
x2 = x1 + hradius;
y2 = y1 + vradius;
x1 -= hradius;
y1 -= vradius;
break;
case CENTER:
hradius = x2 / 2.0f;
vradius = y2 / 2.0f;
x2 = x1 + hradius;
y2 = y1 + vradius;
x1 -= hradius;
y1 -= vradius;
}
if (x2 < x1) {
float temp = x1; x1 = x2; x2 = temp;
}
if (y2 < y1) {
float temp = y1; y1 = y2; y2 = temp;
}

// float currentY = y1;
float boxWidth = x2 - x1;

// // ala illustrator, the text itself must fit inside the box
// currentY += textAscent(); //ascent() * textSize;
// // if the box is already too small, tell em to f off
// if (currentY > y2) return;

float spaceWidth = textWidth(' ');

if (textBreakStart == null) {
textBreakStart = new int[20];
textBreakStop = new int[20];
}
textBreakCount = 0;

int length = str.length();
if (length + 1 > textBuffer.length) {
textBuffer = new char[length + 1];
}
str.getChars(0, length, textBuffer, 0);
// add a fake newline to simplify calculations
textBuffer[length++] = '\n';

int sentenceStart = 0;
for (int i = 0; i < length; i++) {
if (textBuffer[i] == '\n') {
// currentY = textSentence(textBuffer, sentenceStart, i,
// lineX, boxWidth, currentY, y2, spaceWidth);
boolean legit =
textSentence(textBuffer, sentenceStart, i, boxWidth, spaceWidth);
if (!legit) break;
// if (Float.isNaN(currentY)) break; // word too big (or error)
// if (currentY > y2) break; // past the box
sentenceStart = i + 1;
}
}

// lineX is the position where the text starts, which is adjusted
// to left/center/right based on the current textAlign
float lineX = x1; //boxX1;
if (textAlign == CENTER) {
lineX = lineX + boxWidth/2f;
} else if (textAlign == RIGHT) {
lineX = x2; //boxX2;
}

float boxHeight = y2 - y1;
//int lineFitCount = 1 + PApplet.floor((boxHeight - textAscent()) / textLeading);
// incorporate textAscent() for the top (baseline will be y1 + ascent)
// and textDescent() for the bottom, so that lower parts of letters aren't
// outside the box. [0151]
float topAndBottom = textAscent() + textDescent();
int lineFitCount = 1 + PApplet.floor((boxHeight - topAndBottom) / textLeading);
int lineCount = Math.min(textBreakCount, lineFitCount);

if (textAlignY == CENTER) {
float lineHigh = textAscent() + textLeading * (lineCount - 1);
float y = y1 + textAscent() + (boxHeight - lineHigh) / 2;
for (int i = 0; i < lineCount; i++) {
textLineAlignImpl(textBuffer, textBreakStart[i], textBreakStop[i], lineX, y);
y += textLeading;
}

} else if (textAlignY == BOTTOM) {
float y = y2 - textDescent() - textLeading * (lineCount - 1);
for (int i = 0; i < lineCount; i++) {
textLineAlignImpl(textBuffer, textBreakStart[i], textBreakStop[i], lineX, y);
y += textLeading;
}

} else { // TOP or BASELINE just go to the default
float y = y1 + textAscent();
for (int i = 0; i < lineCount; i++) {
textLineAlignImpl(textBuffer, textBreakStart[i], textBreakStop[i], lineX, y);
y += textLeading;
}
}

if (textMode == SCREEN) updatePixels();
}


/**
* Emit a sentence of text, defined as a chunk of text without any newlines.
* @param stop non-inclusive, the end of the text in question
*/
protected boolean textSentence(char[] buffer, int start, int stop,
float boxWidth, float spaceWidth) {
float runningX = 0;

// Keep track of this separately from index, since we'll need to back up
// from index when breaking words that are too long to fit.
int lineStart = start;
int wordStart = start;
int index = start;
while (index <= stop) {
// boundary of a word or end of this sentence
if ((buffer[index] == ' ') || (index == stop)) {
float wordWidth = textWidthImpl(buffer, wordStart, index);

if (runningX + wordWidth > boxWidth) {
if (runningX != 0) {
// Next word is too big, output the current line and advance
index = wordStart;
textSentenceBreak(lineStart, index);
// Eat whitespace because multiple spaces don't count for s*
// when they're at the end of a line.
while ((index < stop) && (buffer[index] == ' ')) {
index++;
}
} else { // (runningX == 0)
// If this is the first word on the line, and its width is greater
// than the width of the text box, then break the word where at the
// max width, and send the rest of the word to the next line.
do {
index--;
if (index == wordStart) {
// Not a single char will fit on this line. screw 'em.
//System.out.println("screw you");
return false; //Float.NaN;
}
wordWidth = textWidthImpl(buffer, wordStart, index);
} while (wordWidth > boxWidth);

//textLineImpl(buffer, lineStart, index, x, y);
textSentenceBreak(lineStart, index);
}
lineStart = index;
wordStart = index;
runningX = 0;

} else if (index == stop) {
// last line in the block, time to unload
//textLineImpl(buffer, lineStart, index, x, y);
textSentenceBreak(lineStart, index);
// y += textLeading;
index++;

} else { // this word will fit, just add it to the line
runningX += wordWidth + spaceWidth;
wordStart = index + 1; // move on to the next word
index++;
}
} else { // not a space or the last character
index++; // this is just another letter
}
}
// return y;
return true;
}


protected void textSentenceBreak(int start, int stop) {
if (textBreakCount == textBreakStart.length) {
textBreakStart = PApplet.expand(textBreakStart);
textBreakStop = PApplet.expand(textBreakStop);
}
textBreakStart[textBreakCount] = start;
textBreakStop[textBreakCount] = stop;
textBreakCount++;
}


public void text(String s, float x1, float y1, float x2, float y2, float z) {
if (z != 0) translate(0, 0, z); // slowness, badness

text(s, x1, y1, x2, y2);
textZ = z;

if (z != 0) translate(0, 0, -z); // TEMPORARY HACK! SLOW!
}


public void text(int num, float x, float y) {
text(String.valueOf(num), x, y);
}


public void text(int num, float x, float y, float z) {
text(String.valueOf(num), x, y, z);
}


/**
* This does a basic number formatting, to avoid the
* generally ugly appearance of printing floats.
* Users who want more control should use their own nf() cmmand,
* or if they want the long, ugly version of float,
* use String.valueOf() to convert the float to a String first.
*/
public void text(float num, float x, float y) {
text(PApplet.nfs(num, 0, 3), x, y);
}


public void text(float num, float x, float y, float z) {
text(PApplet.nfs(num, 0, 3), x, y, z);
}



//////////////////////////////////////////////////////////////

// TEXT IMPL

// These are most likely to be overridden by subclasses, since the other
// (public) functions handle generic features like setting alignment.


/**
* Handles placement of a text line, then calls textLineImpl
* to actually render at the specific point.
*/
protected void textLineAlignImpl(char buffer[], int start, int stop,
float x, float y) {
if (textAlign == CENTER) {
x -= textWidthImpl(buffer, start, stop) / 2f;

} else if (textAlign == RIGHT) {
x -= textWidthImpl(buffer, start, stop);
}

textLineImpl(buffer, start, stop, x, y);
}


/**
* Implementation of actual drawing for a line of text.
*/
protected void textLineImpl(char buffer[], int start, int stop,
float x, float y) {
for (int index = start; index < stop; index++) {
textCharImpl(buffer[index], x, y);

// this doesn't account for kerning
x += textWidth(buffer[index]);
}
textX = x;
textY = y;
textZ = 0; // this will get set by the caller if non-zero
}


protected void textCharImpl(char ch, float x, float y) { //, float z) {
PFont.Glyph glyph = textFont.getGlyph(ch);
if (glyph != null) {
if (textMode == MODEL) {
float high = glyph.height / (float) textFont.size;
float bwidth = glyph.width / (float) textFont.size;
float lextent = glyph.leftExtent / (float) textFont.size;
float textent = glyph.topExtent / (float) textFont.size;

float x1 = x + lextent * textSize;
float y1 = y - textent * textSize;
float x2 = x1 + bwidth * textSize;
float y2 = y1 + high * textSize;

textCharModelImpl(glyph.image,
x1, y1, x2, y2,
glyph.width, glyph.height);

} else if (textMode == SCREEN) {
int xx = (int) x + glyph.leftExtent;
int yy = (int) y - glyph.topExtent;

int w0 = glyph.width;
int h0 = glyph.height;

textCharScreenImpl(glyph.image, xx, yy, w0, h0);
}
}
}


protected void textCharModelImpl(PImage glyph,
float x1, float y1, //float z1,
float x2, float y2, //float z2,
int u2, int v2) {
boolean savedTint = tint;
int savedTintColor = tintColor;
float savedTintR = tintR;
float savedTintG = tintG;
float savedTintB = tintB;
float savedTintA = tintA;
boolean savedTintAlpha = tintAlpha;

tint = true;
tintColor = fillColor;
tintR = fillR;
tintG = fillG;
tintB = fillB;
tintA = fillA;
tintAlpha = fillAlpha;

imageImpl(glyph, x1, y1, x2, y2, 0, 0, u2, v2);

tint = savedTint;
tintColor = savedTintColor;
tintR = savedTintR;
tintG = savedTintG;
tintB = savedTintB;
tintA = savedTintA;
tintAlpha = savedTintAlpha;
}


protected void textCharScreenImpl(PImage glyph,
int xx, int yy,
int w0, int h0) {
int x0 = 0;
int y0 = 0;

if ((xx >= width) || (yy >= height) ||
(xx + w0 < 0) || (yy + h0 < 0)) return;

if (xx < 0) {
x0 -= xx;
w0 += xx;
xx = 0;
}
if (yy < 0) {
y0 -= yy;
h0 += yy;
yy = 0;
}
if (xx + w0 > width) {
w0 -= ((xx + w0) - width);
}
if (yy + h0 > height) {
h0 -= ((yy + h0) - height);
}

int fr = fillRi;
int fg = fillGi;
int fb = fillBi;
int fa = fillAi;

int pixels1[] = glyph.pixels; //images[glyph].pixels;

// TODO this can be optimized a bit
for (int row = y0; row < y0 + h0; row++) {
for (int col = x0; col < x0 + w0; col++) {
//int a1 = (fa * pixels1[row * textFont.twidth + col]) >> 8;
int a1 = (fa * pixels1[row * glyph.width + col]) >> 8;
int a2 = a1 ^ 0xff;
//int p1 = pixels1[row * glyph.width + col];
int p2 = pixels[(yy + row-y0)*width + (xx+col-x0)];

pixels[(yy + row-y0)*width + xx+col-x0] =
(0xff000000 |
(((a1 * fr + a2 * ((p2 >> 16) & 0xff)) & 0xff00) << 8) |
(( a1 * fg + a2 * ((p2 >> 8) & 0xff)) & 0xff00) |
(( a1 * fb + a2 * ( p2 & 0xff)) >> 8));
}
}
}



//////////////////////////////////////////////////////////////

// MATRIX STACK


/**
* Push a copy of the current transformation matrix onto the stack.
*/
public void pushMatrix() {
showMethodWarning("pushMatrix");
}


/**
* Replace the current transformation matrix with the top of the stack.
*/
public void popMatrix() {
showMethodWarning("popMatrix");
}



//////////////////////////////////////////////////////////////

// MATRIX TRANSFORMATIONS


/**
* Translate in X and Y.
*/
public void translate(float tx, float ty) {
showMissingWarning("translate");
}


/**
* Translate in X, Y, and Z.
*/
public void translate(float tx, float ty, float tz) {
showMissingWarning("translate");
}


/**
* Two dimensional rotation.
*
* Same as rotateZ (this is identical to a 3D rotation along the z-axis)
* but included for clarity. It'd be weird for people drawing 2D graphics
* to be using rotateZ. And they might kick our a-- for the confusion.
*
* <A HREF="http://www.xkcd.com/c184.html">Additional background</A>.
*/
public void rotate(float angle) {
showMissingWarning("rotate");
}


/**
* Rotate around the X axis.
*/
public void rotateX(float angle) {
showMethodWarning("rotateX");
}


/**
* Rotate around the Y axis.
*/
public void rotateY(float angle) {
showMethodWarning("rotateY");
}


/**
* Rotate around the Z axis.
*
* The functions rotate() and rotateZ() are identical, it's just that it make
* sense to have rotate() and then rotateX() and rotateY() when using 3D;
* nor does it make sense to use a function called rotateZ() if you're only
* doing things in 2D. so we just decided to have them both be the same.
*/
public void rotateZ(float angle) {
showMethodWarning("rotateZ");
}


/**
* Rotate about a vector in space. Same as the glRotatef() function.
*/
public void rotate(float angle, float vx, float vy, float vz) {
showMissingWarning("rotate");
}


/**
* Scale in all dimensions.
*/
public void scale(float s) {
showMissingWarning("scale");
}


/**
* Scale in X and Y. Equivalent to scale(sx, sy, 1).
*
* Not recommended for use in 3D, because the z-dimension is just
* scaled by 1, since there's no way to know what else to scale it by.
*/
public void scale(float sx, float sy) {
showMissingWarning("scale");
}


/**
* Scale in X, Y, and Z.
*/
public void scale(float x, float y, float z) {
showMissingWarning("scale");
}


/**
* Shear along X axis
*/
public void shearX(float angle) {
showMissingWarning("shearX");
}


/**
* Shear along Y axis
*/
public void shearY(float angle) {
showMissingWarning("shearY");
}


//////////////////////////////////////////////////////////////

// MATRIX FULL MONTY


/**
* Set the current transformation matrix to identity.
*/
public void resetMatrix() {
showMethodWarning("resetMatrix");
}


public void applyMatrix(PMatrix source) {
if (source instanceof PMatrix2D) {
applyMatrix((PMatrix2D) source);
} else if (source instanceof PMatrix3D) {
applyMatrix((PMatrix3D) source);
}
}


public void applyMatrix(PMatrix2D source) {
applyMatrix(source.m00, source.m01, source.m02,
source.m10, source.m11, source.m12);
}


/**
* Apply a 3x2 affine transformation matrix.
*/
public void applyMatrix(float n00, float n01, float n02,
float n10, float n11, float n12) {
showMissingWarning("applyMatrix");
}


public void applyMatrix(PMatrix3D source) {
applyMatrix(source.m00, source.m01, source.m02, source.m03,
source.m10, source.m11, source.m12, source.m13,
source.m20, source.m21, source.m22, source.m23,
source.m30, source.m31, source.m32, source.m33);
}


/**
* Apply a 4x4 transformation matrix.
*/
public void applyMatrix(float n00, float n01, float n02, float n03,
float n10, float n11, float n12, float n13,
float n20, float n21, float n22, float n23,
float n30, float n31, float n32, float n33) {
showMissingWarning("applyMatrix");
}



//////////////////////////////////////////////////////////////

// MATRIX GET/SET/PRINT


public PMatrix getMatrix() {
showMissingWarning("getMatrix");
return null;
}


/**
* Copy the current transformation matrix into the specified target.
* Pass in null to create a new matrix.
*/
public PMatrix2D getMatrix(PMatrix2D target) {
showMissingWarning("getMatrix");
return null;
}


/**
* Copy the current transformation matrix into the specified target.
* Pass in null to create a new matrix.
*/
public PMatrix3D getMatrix(PMatrix3D target) {
showMissingWarning("getMatrix");
return null;
}


/**
* Set the current transformation matrix to the contents of another.
*/
public void setMatrix(PMatrix source) {
if (source instanceof PMatrix2D) {
setMatrix((PMatrix2D) source);
} else if (source instanceof PMatrix3D) {
setMatrix((PMatrix3D) source);
}
}


/**
* Set the current transformation to the contents of the specified source.
*/
public void setMatrix(PMatrix2D source) {
showMissingWarning("setMatrix");
}


/**
* Set the current transformation to the contents of the specified source.
*/
public void setMatrix(PMatrix3D source) {
showMissingWarning("setMatrix");
}


/**
* Print the current model (or "transformation") matrix.
*/
public void printMatrix() {
showMethodWarning("printMatrix");
}


//////////////////////////////////////////////////////////////

// CAMERA


public void beginCamera() {
showMethodWarning("beginCamera");
}


public void endCamera() {
showMethodWarning("endCamera");
}


public void camera() {
showMissingWarning("camera");
}


public void camera(float eyeX, float eyeY, float eyeZ,
float centerX, float centerY, float centerZ,
float upX, float upY, float upZ) {
showMissingWarning("camera");
}


public void printCamera() {
showMethodWarning("printCamera");
}



//////////////////////////////////////////////////////////////

// PROJECTION


public void ortho() {
showMissingWarning("ortho");
}


public void ortho(float left, float right,
float bottom, float top) {
showMissingWarning("ortho");
}


public void ortho(float left, float right,
float bottom, float top,
float near, float far) {
showMissingWarning("ortho");
}


public void perspective() {
showMissingWarning("perspective");
}


public void perspective(float fovy, float aspect, float zNear, float zFar) {
showMissingWarning("perspective");
}


public void frustum(float left, float right,
float bottom, float top,
float near, float far) {
showMethodWarning("frustum");
}


public void printProjection() {
showMethodWarning("printCamera");
}



//////////////////////////////////////////////////////////////

// SCREEN TRANSFORMS


/**
* Given an x and y coordinate, returns the x position of where
* that point would be placed on screen, once affected by translate(),
* scale(), or any other transformations.
*/
public float screenX(float x, float y) {
showMissingWarning("screenX");
return 0;
}


/**
* Given an x and y coordinate, returns the y position of where
* that point would be placed on screen, once affected by translate(),
* scale(), or any other transformations.
*/
public float screenY(float x, float y) {
showMissingWarning("screenY");
return 0;
}


/**
* Maps a three dimensional point to its placement on-screen.
* <P>
* Given an (x, y, z) coordinate, returns the x position of where
* that point would be placed on screen, once affected by translate(),
* scale(), or any other transformations.
*/
public float screenX(float x, float y, float z) {
showMissingWarning("screenX");
return 0;
}


/**
* Maps a three dimensional point to its placement on-screen.
* <P>
* Given an (x, y, z) coordinate, returns the y position of where
* that point would be placed on screen, once affected by translate(),
* scale(), or any other transformations.
*/
public float screenY(float x, float y, float z) {
showMissingWarning("screenY");
return 0;
}


/**
* Maps a three dimensional point to its placement on-screen.
* <P>
* Given an (x, y, z) coordinate, returns its z value.
* This value can be used to determine if an (x, y, z) coordinate
* is in front or in back of another (x, y, z) coordinate.
* The units are based on how the zbuffer is set up, and don't
* relate to anything "real". They're only useful for in
* comparison to another value obtained from screenZ(),
* or directly out of the zbuffer[].
*/
public float screenZ(float x, float y, float z) {
showMissingWarning("screenZ");
return 0;
}


/**
* Returns the model space x value for an x, y, z coordinate.
* <P>
* This will give you a coordinate after it has been transformed
* by translate(), rotate(), and camera(), but not yet transformed
* by the projection matrix. For instance, his can be useful for
* figuring out how points in 3D space relate to the edge
* coordinates of a shape.
*/
public float modelX(float x, float y, float z) {
showMissingWarning("modelX");
return 0;
}


/**
* Returns the model space y value for an x, y, z coordinate.
*/
public float modelY(float x, float y, float z) {
showMissingWarning("modelY");
return 0;
}


/**
* Returns the model space z value for an x, y, z coordinate.
*/
public float modelZ(float x, float y, float z) {
showMissingWarning("modelZ");
return 0;
}



//////////////////////////////////////////////////////////////

// STYLE


public void pushStyle() {
if (styleStackDepth == styleStack.length) {
styleStack = (PStyle[]) PApplet.expand(styleStack);
}
if (styleStack[styleStackDepth] == null) {
styleStack[styleStackDepth] = new PStyle();
}
PStyle s = styleStack[styleStackDepth++];
getStyle(s);
}


public void popStyle() {
if (styleStackDepth == 0) {
throw new RuntimeException("Too many popStyle() without enough pushStyle()");
}
styleStackDepth--;
style(styleStack[styleStackDepth]);
}


public void style(PStyle s) {
// if (s.smooth) {
// smooth();
// } else {
// noSmooth();
// }

imageMode(s.imageMode);
rectMode(s.rectMode);
ellipseMode(s.ellipseMode);
shapeMode(s.shapeMode);

if (s.tint) {
tint(s.tintColor);
} else {
noTint();
}
if (s.fill) {
fill(s.fillColor);
} else {
noFill();
}
if (s.stroke) {
stroke(s.strokeColor);
} else {
noStroke();
}
strokeWeight(s.strokeWeight);
strokeCap(s.strokeCap);
strokeJoin(s.strokeJoin);

// Set the colorMode() for the material properties.
// TODO this is really inefficient, need to just have a material() method,
// but this has the least impact to the API.
colorMode(RGB, 1);
ambient(s.ambientR, s.ambientG, s.ambientB);
emissive(s.emissiveR, s.emissiveG, s.emissiveB);
specular(s.specularR, s.specularG, s.specularB);
shininess(s.shininess);

/*
s.ambientR = ambientR;
s.ambientG = ambientG;
s.ambientB = ambientB;
s.specularR = specularR;
s.specularG = specularG;
s.specularB = specularB;
s.emissiveR = emissiveR;
s.emissiveG = emissiveG;
s.emissiveB = emissiveB;
s.shininess = shininess;
*/
// material(s.ambientR, s.ambientG, s.ambientB,
// s.emissiveR, s.emissiveG, s.emissiveB,
// s.specularR, s.specularG, s.specularB,
// s.shininess);

// Set this after the material properties.
colorMode(s.colorMode,
s.colorModeX, s.colorModeY, s.colorModeZ, s.colorModeA);

// This is a bit asymmetric, since there's no way to do "noFont()",
// and a null textFont will produce an error (since usually that means that
// the font couldn't load properly). So in some cases, the font won't be
// 'cleared' to null, even though that's technically correct.
if (s.textFont != null) {
textFont(s.textFont, s.textSize);
textLeading(s.textLeading);
}
// These don't require a font to be set.
textAlign(s.textAlign, s.textAlignY);
textMode(s.textMode);
}


public PStyle getStyle() { // ignore
return getStyle(null);
}


public PStyle getStyle(PStyle s) { // ignore
if (s == null) {
s = new PStyle();
}

s.imageMode = imageMode;
s.rectMode = rectMode;
s.ellipseMode = ellipseMode;
s.shapeMode = shapeMode;

s.colorMode = colorMode;
s.colorModeX = colorModeX;
s.colorModeY = colorModeY;
s.colorModeZ = colorModeZ;
s.colorModeA = colorModeA;

s.tint = tint;
s.tintColor = tintColor;
s.fill = fill;
s.fillColor = fillColor;
s.stroke = stroke;
s.strokeColor = strokeColor;
s.strokeWeight = strokeWeight;
s.strokeCap = strokeCap;
s.strokeJoin = strokeJoin;

s.ambientR = ambientR;
s.ambientG = ambientG;
s.ambientB = ambientB;
s.specularR = specularR;
s.specularG = specularG;
s.specularB = specularB;
s.emissiveR = emissiveR;
s.emissiveG = emissiveG;
s.emissiveB = emissiveB;
s.shininess = shininess;

s.textFont = textFont;
s.textAlign = textAlign;
s.textAlignY = textAlignY;
s.textMode = textMode;
s.textSize = textSize;
s.textLeading = textLeading;

return s;
}



//////////////////////////////////////////////////////////////

// STROKE CAP/JOIN/WEIGHT


public void strokeWeight(float weight) {
strokeWeight = weight;
}


public void strokeJoin(int join) {
strokeJoin = join;
}


public void strokeCap(int cap) {
strokeCap = cap;
}



//////////////////////////////////////////////////////////////

// STROKE COLOR


/**
* Disables drawing the stroke (outline). If both <b>noStroke()</b> and
* <b>noFill()</b> are called, no shapes will be drawn to the screen.
*
* @webref color:setting
*
* @see PGraphics#stroke(float, float, float, float)
*/
public void noStroke() {
stroke = false;
}


/**
* Set the tint to either a grayscale or ARGB value.
* See notes attached to the fill() function.
* @param rgb color value in hexadecimal notation
* (i.e. #FFCC00 or 0xFFFFCC00) or any value of the color datatype
*/
public void stroke(int rgb) {
colorCalc(rgb);
strokeFromCalc();
}


public void stroke(int rgb, float alpha) {
colorCalc(rgb, alpha);
strokeFromCalc();
}


/**
*
* @param gray specifies a value between white and black
*/
public void stroke(float gray) {
colorCalc(gray);
strokeFromCalc();
}


public void stroke(float gray, float alpha) {
colorCalc(gray, alpha);
strokeFromCalc();
}


public void stroke(float x, float y, float z) {
colorCalc(x, y, z);
strokeFromCalc();
}


/**
* Sets the color used to draw lines and borders around shapes. This color
* is either specified in terms of the RGB or HSB color depending on the
* current <b>colorMode()</b> (the default color space is RGB, with each
* value in the range from 0 to 255).
* <br><br>When using hexadecimal notation to specify a color, use "#" or
* "0x" before the values (e.g. #CCFFAA, 0xFFCCFFAA). The # syntax uses six
* digits to specify a color (the way colors are specified in HTML and CSS).
* When using the hexadecimal notation starting with "0x", the hexadecimal
* value must be specified with eight characters; the first two characters
* define the alpha component and the remainder the red, green, and blue
* components.
* <br><br>The value for the parameter "gray" must be less than or equal
* to the current maximum value as specified by <b>colorMode()</b>.
* The default maximum value is 255.
*
* @webref color:setting
* @param alpha opacity of the stroke
* @param x red or hue value (depending on the current color mode)
* @param y green or saturation value (depending on the current color mode)
* @param z blue or brightness value (depending on the current color mode)
*/
public void stroke(float x, float y, float z, float a) {
colorCalc(x, y, z, a);
strokeFromCalc();
}


protected void strokeFromCalc() {
stroke = true;
strokeR = calcR;
strokeG = calcG;
strokeB = calcB;
strokeA = calcA;
strokeRi = calcRi;
strokeGi = calcGi;
strokeBi = calcBi;
strokeAi = calcAi;
strokeColor = calcColor;
strokeAlpha = calcAlpha;
}



//////////////////////////////////////////////////////////////

// TINT COLOR


/**
* Removes the current fill value for displaying images and reverts to displaying images with their original hues.
*
* @webref image:loading_displaying
* @see processing.core.PGraphics#tint(float, float, float, float)
* @see processing.core.PGraphics#image(PImage, float, float, float, float)
*/
public void noTint() {
tint = false;
}


/**
* Set the tint to either a grayscale or ARGB value.
*/
public void tint(int rgb) {
colorCalc(rgb);
tintFromCalc();
}


/**
* @param rgb color value in hexadecimal notation
* (i.e. #FFCC00 or 0xFFFFCC00) or any value of the color datatype
* @param alpha opacity of the image
*/
public void tint(int rgb, float alpha) {
colorCalc(rgb, alpha);
tintFromCalc();
}


/**
* @param gray any valid number
*/
public void tint(float gray) {
colorCalc(gray);
tintFromCalc();
}


public void tint(float gray, float alpha) {
colorCalc(gray, alpha);
tintFromCalc();
}


public void tint(float x, float y, float z) {
colorCalc(x, y, z);
tintFromCalc();
}


/**
* Sets the fill value for displaying images. Images can be tinted to
* specified colors or made transparent by setting the alpha.
* <br><br>To make an image transparent, but not change it's color,
* use white as the tint color and specify an alpha value. For instance,
* tint(255, 128) will make an image 50% transparent (unless
* <b>colorMode()</b> has been used).
*
* <br><br>When using hexadecimal notation to specify a color, use "#" or
* "0x" before the values (e.g. #CCFFAA, 0xFFCCFFAA). The # syntax uses six
* digits to specify a color (the way colors are specified in HTML and CSS).
* When using the hexadecimal notation starting with "0x", the hexadecimal
* value must be specified with eight characters; the first two characters
* define the alpha component and the remainder the red, green, and blue
* components.
* <br><br>The value for the parameter "gray" must be less than or equal
* to the current maximum value as specified by <b>colorMode()</b>.
* The default maximum value is 255.
* <br><br>The tint() method is also used to control the coloring of
* textures in 3D.
*
* @webref image:loading_displaying
* @param x red or hue value
* @param y green or saturation value
* @param z blue or brightness value
*
* @see processing.core.PGraphics#noTint()
* @see processing.core.PGraphics#image(PImage, float, float, float, float)
*/
public void tint(float x, float y, float z, float a) {
colorCalc(x, y, z, a);
tintFromCalc();
}


protected void tintFromCalc() {
tint = true;
tintR = calcR;
tintG = calcG;
tintB = calcB;
tintA = calcA;
tintRi = calcRi;
tintGi = calcGi;
tintBi = calcBi;
tintAi = calcAi;
tintColor = calcColor;
tintAlpha = calcAlpha;
}



//////////////////////////////////////////////////////////////

// FILL COLOR


/**
* Disables filling geometry. If both <b>noStroke()</b> and <b>noFill()</b>
* are called, no shapes will be drawn to the screen.
*
* @webref color:setting
*
* @see PGraphics#fill(float, float, float, float)
*
*/
public void noFill() {
fill = false;
}


/**
* Set the fill to either a grayscale value or an ARGB int.
* @param rgb color value in hexadecimal notation (i.e. #FFCC00 or 0xFFFFCC00) or any value of the color datatype
*/
public void fill(int rgb) {
colorCalc(rgb);
fillFromCalc();
}


public void fill(int rgb, float alpha) {
colorCalc(rgb, alpha);
fillFromCalc();
}


/**
* @param gray number specifying value between white and black
*/
public void fill(float gray) {
colorCalc(gray);
fillFromCalc();
}


public void fill(float gray, float alpha) {
colorCalc(gray, alpha);
fillFromCalc();
}


public void fill(float x, float y, float z) {
colorCalc(x, y, z);
fillFromCalc();
}


/**
* Sets the color used to fill shapes. For example, if you run <b>fill(204, 102, 0)</b>, all subsequent shapes will be filled with orange. This color is either specified in terms of the RGB or HSB color depending on the current <b>colorMode()</b> (the default color space is RGB, with each value in the range from 0 to 255).
* <br><br>When using hexadecimal notation to specify a color, use "#" or "0x" before the values (e.g. #CCFFAA, 0xFFCCFFAA). The # syntax uses six digits to specify a color (the way colors are specified in HTML and CSS). When using the hexadecimal notation starting with "0x", the hexadecimal value must be specified with eight characters; the first two characters define the alpha component and the remainder the red, green, and blue components.
* <br><br>The value for the parameter "gray" must be less than or equal to the current maximum value as specified by <b>colorMode()</b>. The default maximum value is 255.
* <br><br>To change the color of an image (or a texture), use tint().
*
* @webref color:setting
* @param x red or hue value
* @param y green or saturation value
* @param z blue or brightness value
* @param alpha opacity of the fill
*
* @see PGraphics#noFill()
* @see PGraphics#stroke(float)
* @see PGraphics#tint(float)
* @see PGraphics#background(float, float, float, float)
* @see PGraphics#colorMode(int, float, float, float, float)
*/
public void fill(float x, float y, float z, float a) {
colorCalc(x, y, z, a);
fillFromCalc();
}


protected void fillFromCalc() {
fill = true;
fillR = calcR;
fillG = calcG;
fillB = calcB;
fillA = calcA;
fillRi = calcRi;
fillGi = calcGi;
fillBi = calcBi;
fillAi = calcAi;
fillColor = calcColor;
fillAlpha = calcAlpha;
}



//////////////////////////////////////////////////////////////

// MATERIAL PROPERTIES


public void ambient(int rgb) {
// if (((rgb & 0xff000000) == 0) && (rgb <= colorModeX)) {
// ambient((float) rgb);
//
// } else {
// colorCalcARGB(rgb, colorModeA);
// ambientFromCalc();
// }
colorCalc(rgb);
ambientFromCalc();
}


public void ambient(float gray) {
colorCalc(gray);
ambientFromCalc();
}


public void ambient(float x, float y, float z) {
colorCalc(x, y, z);
ambientFromCalc();
}


protected void ambientFromCalc() {
ambientR = calcR;
ambientG = calcG;
ambientB = calcB;
}


public void specular(int rgb) {
// if (((rgb & 0xff000000) == 0) && (rgb <= colorModeX)) {
// specular((float) rgb);
//
// } else {
// colorCalcARGB(rgb, colorModeA);
// specularFromCalc();
// }
colorCalc(rgb);
specularFromCalc();
}


public void specular(float gray) {
colorCalc(gray);
specularFromCalc();
}


public void specular(float x, float y, float z) {
colorCalc(x, y, z);
specularFromCalc();
}


protected void specularFromCalc() {
specularR = calcR;
specularG = calcG;
specularB = calcB;
}


public void shininess(float shine) {
shininess = shine;
}


public void emissive(int rgb) {
// if (((rgb & 0xff000000) == 0) && (rgb <= colorModeX)) {
// emissive((float) rgb);
//
// } else {
// colorCalcARGB(rgb, colorModeA);
// emissiveFromCalc();
// }
colorCalc(rgb);
emissiveFromCalc();
}


public void emissive(float gray) {
colorCalc(gray);
emissiveFromCalc();
}


public void emissive(float x, float y, float z) {
colorCalc(x, y, z);
emissiveFromCalc();
}


protected void emissiveFromCalc() {
emissiveR = calcR;
emissiveG = calcG;
emissiveB = calcB;
}



//////////////////////////////////////////////////////////////

// LIGHTS

// The details of lighting are very implementation-specific, so this base
// class does not handle any details of settings lights. It does however
// display warning messages that the functions are not available.


public void lights() {
showMethodWarning("lights");
}

public void noLights() {
showMethodWarning("noLights");
}

public void ambientLight(float red, float green, float blue) {
showMethodWarning("ambientLight");
}

public void ambientLight(float red, float green, float blue,
float x, float y, float z) {
showMethodWarning("ambientLight");
}

public void directionalLight(float red, float green, float blue,
float nx, float ny, float nz) {
showMethodWarning("directionalLight");
}

public void pointLight(float red, float green, float blue,
float x, float y, float z) {
showMethodWarning("pointLight");
}

public void spotLight(float red, float green, float blue,
float x, float y, float z,
float nx, float ny, float nz,
float angle, float concentration) {
showMethodWarning("spotLight");
}

public void lightFalloff(float constant, float linear, float quadratic) {
showMethodWarning("lightFalloff");
}

public void lightSpecular(float x, float y, float z) {
showMethodWarning("lightSpecular");
}



//////////////////////////////////////////////////////////////

// BACKGROUND


/**
* Set the background to a gray or ARGB color.
* <p>
* For the main drawing surface, the alpha value will be ignored. However,
* alpha can be used on PGraphics objects from createGraphics(). This is
* the only way to set all the pixels partially transparent, for instance.
* <p>
* Note that background() should be called before any transformations occur,
* because some implementations may require the current transformation matrix
* to be identity before drawing.
*
* @param rgb color value in hexadecimal notation (i.e. #FFCC00 or 0xFFFFCC00)<br/>or any value of the color datatype
*/
public void background(int rgb) {
// if (((rgb & 0xff000000) == 0) && (rgb <= colorModeX)) {
// background((float) rgb);
//
// } else {
// if (format == RGB) {
// rgb |= 0xff000000; // ignore alpha for main drawing surface
// }
// colorCalcARGB(rgb, colorModeA);
// backgroundFromCalc();
// backgroundImpl();
// }
colorCalc(rgb);
backgroundFromCalc();
}


/**
* See notes about alpha in background(x, y, z, a).
*/
public void background(int rgb, float alpha) {
// if (format == RGB) {
// background(rgb); // ignore alpha for main drawing surface
//
// } else {
// if (((rgb & 0xff000000) == 0) && (rgb <= colorModeX)) {
// background((float) rgb, alpha);
//
// } else {
// colorCalcARGB(rgb, alpha);
// backgroundFromCalc();
// backgroundImpl();
// }
// }
colorCalc(rgb, alpha);
backgroundFromCalc();
}


/**
* Set the background to a grayscale value, based on the
* current colorMode.
*/
public void background(float gray) {
colorCalc(gray);
backgroundFromCalc();
// backgroundImpl();
}


/**
* See notes about alpha in background(x, y, z, a).
* @param gray specifies a value between white and black
* @param alpha opacity of the background
*/
public void background(float gray, float alpha) {
if (format == RGB) {
background(gray); // ignore alpha for main drawing surface

} else {
colorCalc(gray, alpha);
backgroundFromCalc();
// backgroundImpl();
}
}


/**
* Set the background to an r, g, b or h, s, b value,
* based on the current colorMode.
*/
public void background(float x, float y, float z) {
colorCalc(x, y, z);
backgroundFromCalc();
// backgroundImpl();
}


/**
* The <b>background()</b> function sets the color used for the background of the Processing window. The default background is light gray. In the <b>draw()</b> function, the background color is used to clear the display window at the beginning of each frame.
* <br><br>An image can also be used as the background for a sketch, however its width and height must be the same size as the sketch window. To resize an image 'b' to the size of the sketch window, use b.resize(width, height).
* <br><br>Images used as background will ignore the current tint() setting.
* <br><br>It is not possible to use transparency (alpha) in background colors with the main drawing surface, however they will work properly with <b>createGraphics</b>.
*
* =advanced
* <p>Clear the background with a color that includes an alpha value. This can
* only be used with objects created by createGraphics(), because the main
* drawing surface cannot be set transparent.</p>
* <p>It might be tempting to use this function to partially clear the screen
* on each frame, however that's not how this function works. When calling
* background(), the pixels will be replaced with pixels that have that level
* of transparency. To do a semi-transparent overlay, use fill() with alpha
* and draw a rectangle.</p>
*
* @webref color:setting
* @param x red or hue value (depending on the current color mode)
* @param y green or saturation value (depending on the current color mode)
* @param z blue or brightness value (depending on the current color mode)
*
* @see PGraphics#stroke(float)
* @see PGraphics#fill(float)
* @see PGraphics#tint(float)
* @see PGraphics#colorMode(int)
*/
public void background(float x, float y, float z, float a) {
// if (format == RGB) {
// background(x, y, z); // don't allow people to set alpha
//
// } else {
// colorCalc(x, y, z, a);
// backgroundFromCalc();
// backgroundImpl();
// }
colorCalc(x, y, z, a);
backgroundFromCalc();
}


protected void backgroundFromCalc() {
backgroundR = calcR;
backgroundG = calcG;
backgroundB = calcB;
backgroundA = (format == RGB) ? colorModeA : calcA;
backgroundRi = calcRi;
backgroundGi = calcGi;
backgroundBi = calcBi;
backgroundAi = (format == RGB) ? 255 : calcAi;
backgroundAlpha = (format == RGB) ? false : calcAlpha;
backgroundColor = calcColor;

backgroundImpl();
}


/**
* Takes an RGB or ARGB image and sets it as the background.
* The width and height of the image must be the same size as the sketch.
* Use image.resize(width, height) to make short work of such a task.
* <P>
* Note that even if the image is set as RGB, the high 8 bits of each pixel
* should be set opaque (0xFF000000), because the image data will be copied
* directly to the screen, and non-opaque background images may have strange
* behavior. Using image.filter(OPAQUE) will handle this easily.
* <P>
* When using 3D, this will also clear the zbuffer (if it exists).
*/
public void background(PImage image) {
if ((image.width != width) || (image.height != height)) {
throw new RuntimeException(ERROR_BACKGROUND_IMAGE_SIZE);
}
if ((image.format != RGB) && (image.format != ARGB)) {
throw new RuntimeException(ERROR_BACKGROUND_IMAGE_FORMAT);
}
backgroundColor = 0; // just zero it out for images
backgroundImpl(image);
}


/**
* Actually set the background image. This is separated from the error
* handling and other semantic goofiness that is shared across renderers.
*/
protected void backgroundImpl(PImage image) {
// blit image to the screen
set(0, 0, image);
}


/**
* Actual implementation of clearing the background, now that the
* internal variables for background color have been set. Called by the
* backgroundFromCalc() method, which is what all the other background()
* methods call once the work is done.
*/
protected void backgroundImpl() {
pushStyle();
pushMatrix();
resetMatrix();
fill(backgroundColor);
rect(0, 0, width, height);
popMatrix();
popStyle();
}


/**
* Callback to handle clearing the background when begin/endRaw is in use.
* Handled as separate function for OpenGL (or other) subclasses that
* override backgroundImpl() but still needs this to work properly.
*/
// protected void backgroundRawImpl() {
// if (raw != null) {
// raw.colorMode(RGB, 1);
// raw.noStroke();
// raw.fill(backgroundR, backgroundG, backgroundB);
// raw.beginShape(TRIANGLES);
//
// raw.vertex(0, 0);
// raw.vertex(width, 0);
// raw.vertex(0, height);
//
// raw.vertex(width, 0);
// raw.vertex(width, height);
// raw.vertex(0, height);
//
// raw.endShape();
// }
// }



//////////////////////////////////////////////////////////////

// COLOR MODE


/**
* @param mode Either RGB or HSB, corresponding to Red/Green/Blue and Hue/Saturation/Brightness
* @param max range for all color elements
*/
public void colorMode(int mode) {
colorMode(mode, colorModeX, colorModeY, colorModeZ, colorModeA);
}


public void colorMode(int mode, float max) {
colorMode(mode, max, max, max, max);
}


/**
* Set the colorMode and the maximum values for (r, g, b)
* or (h, s, b).
* <P>
* Note that this doesn't set the maximum for the alpha value,
* which might be confusing if for instance you switched to
* <PRE>colorMode(HSB, 360, 100, 100);</PRE>
* because the alpha values were still between 0 and 255.
*/
public void colorMode(int mode, float maxX, float maxY, float maxZ) {
colorMode(mode, maxX, maxY, maxZ, colorModeA);
}


/**
* Changes the way Processing interprets color data. By default, the parameters for <b>fill()</b>, <b>stroke()</b>, <b>background()</b>, and <b>color()</b> are defined by values between 0 and 255 using the RGB color model. The <b>colorMode()</b> function is used to change the numerical range used for specifying colors and to switch color systems. For example, calling <b>colorMode(RGB, 1.0)</b> will specify that values are specified between 0 and 1. The limits for defining colors are altered by setting the parameters range1, range2, range3, and range 4.
*
* @webref color:setting
* @param maxX range for the red or hue depending on the current color mode
* @param maxY range for the green or saturation depending on the current color mode
* @param maxZ range for the blue or brightness depending on the current color mode
* @param maxA range for the alpha
*
* @see PGraphics#background(float)
* @see PGraphics#fill(float)
* @see PGraphics#stroke(float)
*/
public void colorMode(int mode,
float maxX, float maxY, float maxZ, float maxA) {
colorMode = mode;

colorModeX = maxX; // still needs to be set for hsb
colorModeY = maxY;
colorModeZ = maxZ;
colorModeA = maxA;

// if color max values are all 1, then no need to scale
colorModeScale =
((maxA != 1) || (maxX != maxY) || (maxY != maxZ) || (maxZ != maxA));

// if color is rgb/0..255 this will make it easier for the
// red() green() etc functions
colorModeDefault = (colorMode == RGB) &&
(colorModeA == 255) && (colorModeX == 255) &&
(colorModeY == 255) && (colorModeZ == 255);
}



//////////////////////////////////////////////////////////////

// COLOR CALCULATIONS

// Given input values for coloring, these functions will fill the calcXxxx
// variables with values that have been properly filtered through the
// current colorMode settings.

// Renderers that need to subclass any drawing properties such as fill or
// stroke will usally want to override methods like fillFromCalc (or the
// same for stroke, ambient, etc.) That way the color calcuations are
// covered by this based PGraphics class, leaving only a single function
// to override/implement in the subclass.


/**
* Set the fill to either a grayscale value or an ARGB int.
* <P>
* The problem with this code is that it has to detect between these two
* situations automatically. This is done by checking to see if the high bits
* (the alpha for 0xAA000000) is set, and if not, whether the color value
* that follows is less than colorModeX (first param passed to colorMode).
* <P>
* This auto-detect would break in the following situation:
* <PRE>size(256, 256);
* for (int i = 0; i < 256; i++) {
* color c = color(0, 0, 0, i);
* stroke(c);
* line(i, 0, i, 256);
* }</PRE>
* ...on the first time through the loop, where (i == 0), since the color
* itself is zero (black) then it would appear indistinguishable from code
* that reads "fill(0)". The solution is to use the four parameter versions
* of stroke or fill to more directly specify the desired result.
*/
protected void colorCalc(int rgb) {
if (((rgb & 0xff000000) == 0) && (rgb <= colorModeX)) {
colorCalc((float) rgb);

} else {
colorCalcARGB(rgb, colorModeA);
}
}


protected void colorCalc(int rgb, float alpha) {
if (((rgb & 0xff000000) == 0) && (rgb <= colorModeX)) { // see above
colorCalc((float) rgb, alpha);

} else {
colorCalcARGB(rgb, alpha);
}
}


protected void colorCalc(float gray) {
colorCalc(gray, colorModeA);
}


protected void colorCalc(float gray, float alpha) {
if (gray > colorModeX) gray = colorModeX;
if (alpha > colorModeA) alpha = colorModeA;

if (gray < 0) gray = 0;
if (alpha < 0) alpha = 0;

calcR = colorModeScale ? (gray / colorModeX) : gray;
calcG = calcR;
calcB = calcR;
calcA = colorModeScale ? (alpha / colorModeA) : alpha;

calcRi = (int)(calcR*255); calcGi = (int)(calcG*255);
calcBi = (int)(calcB*255); calcAi = (int)(calcA*255);
calcColor = (calcAi << 24) | (calcRi << 16) | (calcGi << 8) | calcBi;
calcAlpha = (calcAi != 255);
}


protected void colorCalc(float x, float y, float z) {
colorCalc(x, y, z, colorModeA);
}


protected void colorCalc(float x, float y, float z, float a) {
if (x > colorModeX) x = colorModeX;
if (y > colorModeY) y = colorModeY;
if (z > colorModeZ) z = colorModeZ;
if (a > colorModeA) a = colorModeA;

if (x < 0) x = 0;
if (y < 0) y = 0;
if (z < 0) z = 0;
if (a < 0) a = 0;

switch (colorMode) {
case RGB:
if (colorModeScale) {
calcR = x / colorModeX;
calcG = y / colorModeY;
calcB = z / colorModeZ;
calcA = a / colorModeA;
} else {
calcR = x; calcG = y; calcB = z; calcA = a;
}
break;

case HSB:
x /= colorModeX; // h
y /= colorModeY; // s
z /= colorModeZ; // b

calcA = colorModeScale ? (a/colorModeA) : a;

if (y == 0) { // saturation == 0
calcR = calcG = calcB = z;

} else {
float which = (x - (int)x) * 6.0f;
float f = which - (int)which;
float p = z * (1.0f - y);
float q = z * (1.0f - y * f);
float t = z * (1.0f - (y * (1.0f - f)));

switch ((int)which) {
case 0: calcR = z; calcG = t; calcB = p; break;
case 1: calcR = q; calcG = z; calcB = p; break;
case 2: calcR = p; calcG = z; calcB = t; break;
case 3: calcR = p; calcG = q; calcB = z; break;
case 4: calcR = t; calcG = p; calcB = z; break;
case 5: calcR = z; calcG = p; calcB = q; break;
}
}
break;
}
calcRi = (int)(255*calcR); calcGi = (int)(255*calcG);
calcBi = (int)(255*calcB); calcAi = (int)(255*calcA);
calcColor = (calcAi << 24) | (calcRi << 16) | (calcGi << 8) | calcBi;
calcAlpha = (calcAi != 255);
}


/**
* Unpacks AARRGGBB color for direct use with colorCalc.
* <P>
* Handled here with its own function since this is indepenent
* of the color mode.
* <P>
* Strangely the old version of this code ignored the alpha
* value. not sure if that was a bug or what.
* <P>
* Note, no need for a bounds check since it's a 32 bit number.
*/
protected void colorCalcARGB(int argb, float alpha) {
if (alpha == colorModeA) {
calcAi = (argb >> 24) & 0xff;
calcColor = argb;
} else {
calcAi = (int) (((argb >> 24) & 0xff) * (alpha / colorModeA));
calcColor = (calcAi << 24) | (argb & 0xFFFFFF);
}
calcRi = (argb >> 16) & 0xff;
calcGi = (argb >> 8) & 0xff;
calcBi = argb & 0xff;
calcA = (float)calcAi / 255.0f;
calcR = (float)calcRi / 255.0f;
calcG = (float)calcGi / 255.0f;
calcB = (float)calcBi / 255.0f;
calcAlpha = (calcAi != 255);
}



//////////////////////////////////////////////////////////////

// COLOR DATATYPE STUFFING

// The 'color' primitive type in Processing syntax is in fact a 32-bit int.
// These functions handle stuffing color values into a 32-bit cage based
// on the current colorMode settings.

// These functions are really slow (because they take the current colorMode
// into account), but they're easy to use. Advanced users can write their
// own bit shifting operations to setup 'color' data types.


public final int color(int c) { // ignore
// if (((c & 0xff000000) == 0) && (c <= colorModeX)) {
// if (colorModeDefault) {
// // bounds checking to make sure the numbers aren't to high or low
// if (c > 255) c = 255; else if (c < 0) c = 0;
// return 0xff000000 | (c << 16) | (c << 8) | c;
// } else {
// colorCalc(c);
// }
// } else {
// colorCalcARGB(c, colorModeA);
// }
colorCalc(c);
return calcColor;
}


public final int color(float gray) { // ignore
colorCalc(gray);
return calcColor;
}


/**
* @param c can be packed ARGB or a gray in this case
*/
public final int color(int c, int alpha) { // ignore
// if (colorModeDefault) {
// // bounds checking to make sure the numbers aren't to high or low
// if (c > 255) c = 255; else if (c < 0) c = 0;
// if (alpha > 255) alpha = 255; else if (alpha < 0) alpha = 0;
//
// return ((alpha & 0xff) << 24) | (c << 16) | (c << 8) | c;
// }
colorCalc(c, alpha);
return calcColor;
}


/**
* @param c can be packed ARGB or a gray in this case
*/
public final int color(int c, float alpha) { // ignore
// if (((c & 0xff000000) == 0) && (c <= colorModeX)) {
colorCalc(c, alpha);
// } else {
// colorCalcARGB(c, alpha);
// }
return calcColor;
}


public final int color(float gray, float alpha) { // ignore
colorCalc(gray, alpha);
return calcColor;
}


public final int color(int x, int y, int z) { // ignore
// if (colorModeDefault) {
// // bounds checking to make sure the numbers aren't to high or low
// if (x > 255) x = 255; else if (x < 0) x = 0;
// if (y > 255) y = 255; else if (y < 0) y = 0;
// if (z > 255) z = 255; else if (z < 0) z = 0;
//
// return 0xff000000 | (x << 16) | (y << 8) | z;
// }
colorCalc(x, y, z);
return calcColor;
}


public final int color(float x, float y, float z) { // ignore
colorCalc(x, y, z);
return calcColor;
}


public final int color(int x, int y, int z, int a) { // ignore
// if (colorModeDefault) {
// // bounds checking to make sure the numbers aren't to high or low
// if (a > 255) a = 255; else if (a < 0) a = 0;
// if (x > 255) x = 255; else if (x < 0) x = 0;
// if (y > 255) y = 255; else if (y < 0) y = 0;
// if (z > 255) z = 255; else if (z < 0) z = 0;
//
// return (a << 24) | (x << 16) | (y << 8) | z;
// }
colorCalc(x, y, z, a);
return calcColor;
}


public final int color(float x, float y, float z, float a) { // ignore
colorCalc(x, y, z, a);
return calcColor;
}



//////////////////////////////////////////////////////////////

// COLOR DATATYPE EXTRACTION

// Vee have veys of making the colors talk.


/**
* Extracts the alpha value from a color.
*
* @webref color:creating_reading
* @param what any value of the color datatype
*/
public final float alpha(int what) {
float c = (what >> 24) & 0xff;
if (colorModeA == 255) return c;
return (c / 255.0f) * colorModeA;
}


/**
* Extracts the red value from a color, scaled to match current <b>colorMode()</b>. This value is always returned as a float so be careful not to assign it to an int value.<br><br>The red() function is easy to use and undestand, but is slower than another technique. To achieve the same results when working in <b>colorMode(RGB, 255)</b>, but with greater speed, use the &gt;&gt; (right shift) operator with a bit mask. For example, the following two lines of code are equivalent:<br><pre>float r1 = red(myColor);<br>float r2 = myColor &gt;&gt; 16 &amp; 0xFF;</pre>
*
* @webref color:creating_reading
* @param what any value of the color datatype
*
* @see PGraphics#green(int)
* @see PGraphics#blue(int)
* @see PGraphics#hue(int)
* @see PGraphics#saturation(int)
* @see PGraphics#brightness(int)
* @ref rightshift
*/
public final float red(int what) {
float c = (what >> 16) & 0xff;
if (colorModeDefault) return c;
return (c / 255.0f) * colorModeX;
}


/**
* Extracts the green value from a color, scaled to match current <b>colorMode()</b>. This value is always returned as a float so be careful not to assign it to an int value.<br><br>The <b>green()</b> function is easy to use and undestand, but is slower than another technique. To achieve the same results when working in <b>colorMode(RGB, 255)</b>, but with greater speed, use the &gt;&gt; (right shift) operator with a bit mask. For example, the following two lines of code are equivalent:<br><pre>float r1 = green(myColor);<br>float r2 = myColor &gt;&gt; 8 &amp; 0xFF;</pre>
*
* @webref color:creating_reading
* @param what any value of the color datatype
*
* @see PGraphics#red(int)
* @see PGraphics#blue(int)
* @see PGraphics#hue(int)
* @see PGraphics#saturation(int)
* @see PGraphics#brightness(int)
* @ref rightshift
*/
public final float green(int what) {
float c = (what >> 8) & 0xff;
if (colorModeDefault) return c;
return (c / 255.0f) * colorModeY;
}


/**
* Extracts the blue value from a color, scaled to match current <b>colorMode()</b>. This value is always returned as a float so be careful not to assign it to an int value.<br><br>The <b>blue()</b> function is easy to use and undestand, but is slower than another technique. To achieve the same results when working in <b>colorMode(RGB, 255)</b>, but with greater speed, use a bit mask to remove the other color components. For example, the following two lines of code are equivalent:<br><pre>float r1 = blue(myColor);<br>float r2 = myColor &amp; 0xFF;</pre>
*
* @webref color:creating_reading
* @param what any value of the color datatype
*
* @see PGraphics#red(int)
* @see PGraphics#green(int)
* @see PGraphics#hue(int)
* @see PGraphics#saturation(int)
* @see PGraphics#brightness(int)
*/
public final float blue(int what) {
float c = (what) & 0xff;
if (colorModeDefault) return c;
return (c / 255.0f) * colorModeZ;
}


/**
* Extracts the hue value from a color.
*
* @webref color:creating_reading
* @param what any value of the color datatype
*
* @see PGraphics#red(int)
* @see PGraphics#green(int)
* @see PGraphics#blue(int)
* @see PGraphics#saturation(int)
* @see PGraphics#brightness(int)
*/
public final float hue(int what) {
if (what != cacheHsbKey) {
Color.RGBtoHSB((what >> 16) & 0xff, (what >> 8) & 0xff,
what & 0xff, cacheHsbValue);
cacheHsbKey = what;
}
return cacheHsbValue[0] * colorModeX;
}


/**
* Extracts the saturation value from a color.
*
* @webref color:creating_reading
* @param what any value of the color datatype
*
* @see PGraphics#red(int)
* @see PGraphics#green(int)
* @see PGraphics#blue(int)
* @see PGraphics#hue(int)
* @see PGraphics#brightness(int)
*/
public final float saturation(int what) {
if (what != cacheHsbKey) {
Color.RGBtoHSB((what >> 16) & 0xff, (what >> 8) & 0xff,
what & 0xff, cacheHsbValue);
cacheHsbKey = what;
}
return cacheHsbValue[1] * colorModeY;
}


/**
* Extracts the brightness value from a color.
*
*
* @webref color:creating_reading
* @param what any value of the color datatype
*
* @see PGraphics#red(int)
* @see PGraphics#green(int)
* @see PGraphics#blue(int)
* @see PGraphics#hue(int)
* @see PGraphics#saturation(int)
*/
public final float brightness(int what) {
if (what != cacheHsbKey) {
Color.RGBtoHSB((what >> 16) & 0xff, (what >> 8) & 0xff,
what & 0xff, cacheHsbValue);
cacheHsbKey = what;
}
return cacheHsbValue[2] * colorModeZ;
}



//////////////////////////////////////////////////////////////

// COLOR DATATYPE INTERPOLATION

// Against our better judgement.


/**
* Calculates a color or colors between two color at a specific increment. The <b>amt</b> parameter is the amount to interpolate between the two values where 0.0 equal to the first point, 0.1 is very near the first point, 0.5 is half-way in between, etc.
*
* @webref color:creating_reading
* @param c1 interpolate from this color
* @param c2 interpolate to this color
* @param amt between 0.0 and 1.0
*
* @see PGraphics#blendColor(int, int, int)
* @see PGraphics#color(float, float, float, float)
*/
public int lerpColor(int c1, int c2, float amt) {
return lerpColor(c1, c2, amt, colorMode);
}

static float[] lerpColorHSB1;
static float[] lerpColorHSB2;

/**
* Interpolate between two colors. Like lerp(), but for the
* individual color components of a color supplied as an int value.
*/
static public int lerpColor(int c1, int c2, float amt, int mode) {
if (mode == RGB) {
float a1 = ((c1 >> 24) & 0xff);
float r1 = (c1 >> 16) & 0xff;
float g1 = (c1 >> 8) & 0xff;
float b1 = c1 & 0xff;
float a2 = (c2 >> 24) & 0xff;
float r2 = (c2 >> 16) & 0xff;
float g2 = (c2 >> 8) & 0xff;
float b2 = c2 & 0xff;

return (((int) (a1 + (a2-a1)*amt) << 24) |
((int) (r1 + (r2-r1)*amt) << 16) |
((int) (g1 + (g2-g1)*amt) << 8) |
((int) (b1 + (b2-b1)*amt)));

} else if (mode == HSB) {
if (lerpColorHSB1 == null) {
lerpColorHSB1 = new float[3];
lerpColorHSB2 = new float[3];
}

float a1 = (c1 >> 24) & 0xff;
float a2 = (c2 >> 24) & 0xff;
int alfa = ((int) (a1 + (a2-a1)*amt)) << 24;

Color.RGBtoHSB((c1 >> 16) & 0xff, (c1 >> 8) & 0xff, c1 & 0xff,
lerpColorHSB1);
Color.RGBtoHSB((c2 >> 16) & 0xff, (c2 >> 8) & 0xff, c2 & 0xff,
lerpColorHSB2);

/* If mode is HSB, this will take the shortest path around the
* color wheel to find the new color. For instance, red to blue
* will go red violet blue (backwards in hue space) rather than
* cycling through ROYGBIV.
*/
// Disabling rollover (wasn't working anyway) for 0126.
// Otherwise it makes full spectrum scale impossible for
// those who might want it...in spite of how despicable
// a full spectrum scale might be.
// roll around when 0.9 to 0.1
// more than 0.5 away means that it should roll in the other direction
/*
float h1 = lerpColorHSB1[0];
float h2 = lerpColorHSB2[0];
if (Math.abs(h1 - h2) > 0.5f) {
if (h1 > h2) {
// i.e. h1 is 0.7, h2 is 0.1
h2 += 1;
} else {
// i.e. h1 is 0.1, h2 is 0.7
h1 += 1;
}
}
float ho = (PApplet.lerp(lerpColorHSB1[0], lerpColorHSB2[0], amt)) % 1.0f;
*/
float ho = PApplet.lerp(lerpColorHSB1[0], lerpColorHSB2[0], amt);
float so = PApplet.lerp(lerpColorHSB1[1], lerpColorHSB2[1], amt);
float bo = PApplet.lerp(lerpColorHSB1[2], lerpColorHSB2[2], amt);

return alfa | (Color.HSBtoRGB(ho, so, bo) & 0xFFFFFF);
}
return 0;
}



//////////////////////////////////////////////////////////////

// BEGINRAW/ENDRAW


/**
* Record individual lines and triangles by echoing them to another renderer.
*/
public void beginRaw(PGraphics rawGraphics) { // ignore
this.raw = rawGraphics;
rawGraphics.beginDraw();
}


public void endRaw() { // ignore
if (raw != null) {
// for 3D, need to flush any geometry that's been stored for sorting
// (particularly if the ENABLE_DEPTH_SORT hint is set)
flush();

// just like beginDraw, this will have to be called because
// endDraw() will be happening outside of draw()
raw.endDraw();
raw.dispose();
raw = null;
}
}



//////////////////////////////////////////////////////////////

// WARNINGS and EXCEPTIONS


static protected HashMap<String, Object> warnings;


/**
* Show a renderer error, and keep track of it so that it's only shown once.
* @param msg the error message (which will be stored for later comparison)
*/
static public void showWarning(String msg) { // ignore
if (warnings == null) {
warnings = new HashMap<String, Object>();
}
if (!warnings.containsKey(msg)) {
System.err.println(msg);
warnings.put(msg, new Object());
}
}


/**
* Display a warning that the specified method is only available with 3D.
* @param method The method name (no parentheses)
*/
static public void showDepthWarning(String method) {
showWarning(method + "() can only be used with a renderer that " +
"supports 3D, such as P3D or OPENGL.");
}


/**
* Display a warning that the specified method that takes x, y, z parameters
* can only be used with x and y parameters in this renderer.
* @param method The method name (no parentheses)
*/
static public void showDepthWarningXYZ(String method) {
showWarning(method + "() with x, y, and z coordinates " +
"can only be used with a renderer that " +
"supports 3D, such as P3D or OPENGL. " +
"Use a version without a z-coordinate instead.");
}


/**
* Display a warning that the specified method is simply unavailable.
*/
static public void showMethodWarning(String method) {
showWarning(method + "() is not available with this renderer.");
}


/**
* Error that a particular variation of a method is unavailable (even though
* other variations are). For instance, if vertex(x, y, u, v) is not
* available, but vertex(x, y) is just fine.
*/
static public void showVariationWarning(String str) {
showWarning(str + " is not available with this renderer.");
}


/**
* Display a warning that the specified method is not implemented, meaning
* that it could be either a completely missing function, although other
* variations of it may still work properly.
*/
static public void showMissingWarning(String method) {
showWarning(method + "(), or this particular variation of it, " +
"is not available with this renderer.");
}


/**
* Show an renderer-related exception that halts the program. Currently just
* wraps the message as a RuntimeException and throws it, but might do
* something more specific might be used in the future.
*/
static public void showException(String msg) { // ignore
throw new RuntimeException(msg);
}


/**
* Same as below, but defaults to a 12 point font, just as MacWrite intended.
*/
protected void defaultFontOrDeath(String method) {
defaultFontOrDeath(method, 12);
}


/**
* First try to create a default font, but if that's not possible, throw
* an exception that halts the program because textFont() has not been used
* prior to the specified method.
*/
protected void defaultFontOrDeath(String method, float size) {
if (parent != null) {
textFont = parent.createDefaultFont(size);
} else {
throw new RuntimeException("Use textFont() before " + method + "()");
}
}



//////////////////////////////////////////////////////////////

// RENDERER SUPPORT QUERIES


/**
* Return true if this renderer should be drawn to the screen. Defaults to
* returning true, since nearly all renderers are on-screen beasts. But can
* be overridden for subclasses like PDF so that a window doesn't open up.
* <br/> <br/>
* A better name? showFrame, displayable, isVisible, visible, shouldDisplay,
* what to call this?
*/
public boolean displayable() {
return true;
}


/**
* Return true if this renderer supports 2D drawing. Defaults to true.
*/
public boolean is2D() {
return true;
}


/**
* Return true if this renderer supports 3D drawing. Defaults to false.
*/
public boolean is3D() {
return false;
}

//////////////////////////////////////////////////////////////

// New API:

protected String[] getSupportedShapeFormats() {
showMissingWarning("getSupportedShapeFormats");
return null;
}


protected PShape loadShape(String filename, Object params) {
showMissingWarning("loadShape");
return null;
}


protected PShape createShape(int size, Object params) {
showMissingWarning("createShape");
return null;
}


public void screenBlend(int mode) {
showMissingWarning("screenBlend");
}


public void textureBlend(int mode) {
showMissingWarning("textureBlend");
}


public PShape beginRecord() { // ignore
showMissingWarning("beginRecord");
return null;
}


public void endRecord() { // ignore
showMissingWarning("endRecord");
}


public boolean isRecording() {
showMissingWarning("isRecording");
return false;
}


public void mergeShapes(boolean val) {
showMissingWarning("mergeShapes");
}


public void shapeName(String name) {
showMissingWarning("shapeName");
}


public void autoNormal(boolean auto) {
this.autoNormal = auto;
}


public void matrixMode(int mode) {
showMissingWarning("matrixMode");
}


public void beginText() {
showMissingWarning("beginText");
}


public void endText() {
showMissingWarning("endText");
}


public void texture(PImage... images) {
showMissingWarning("texture");
}


public void vertex(float x, float y, float... uv) {
showMissingWarning("vertex");
}


public void vertex(float x, float y, float z, float... uv) {
showMissingWarning("vertex");
}
}

Change log

r7761 by andres.colubri on Yesterday (21 hours ago)   Diff
Fixed showMissingWarning() messages in
Processing core
Go to: 

Older revisions

r7750 by f...@processing.org on Yesterday (24 hours ago)   Diff
clean up the last of the font/pdf
fixes
r7716 by andres.colubri on Feb 27, 2011   Diff
Added ortho(legt, right, top, bottom)
function. It uses cameraNear,
cameraFar as the near, far values
r7707 by andres.colubri on Feb 22, 2011   Diff
Multitexture API uses varargs
All revisions of this file

File info

Size: 182977 bytes, 6019 lines
Powered by Google Project Hosting