Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6581 | leency | 1 | --BRUSH: Find AA-colors from pencolors |
2 | --by Richard Fhager |
||
3 | --http://hem.fyristorg.com/dawnbringer/ |
||
4 | |||
5 | -- Copyright 2010 Richard Fhager |
||
6 | -- |
||
7 | -- This program is free software; you can redistribute it and/or |
||
8 | -- modify it under the terms of the GNU General Public License |
||
9 | -- as published by the Free Software Foundation; version 2 |
||
10 | -- of the License. See |
||
11 | |||
12 | cellw = 8 |
||
13 | cellh = 4 |
||
14 | colors = 256 |
||
15 | |||
16 | setbrushsize(cellw * 3, cellh * 3) |
||
17 | |||
18 | |||
19 | -- |
||
20 | function makePalList(cols) |
||
21 | pal = {} |
||
22 | for n = 0, cols-1, 1 do |
||
23 | r,g,b = getcolor(n) |
||
24 | pal[n+1] = {r,g,b} |
||
25 | end |
||
26 | return pal |
||
27 | end |
||
28 | -- |
||
29 | |||
30 | -- |
||
31 | function getBestPalMatchHYBRID(rgb,pal,briweight) |
||
32 | local diff,diffC,diffB,best,bestcol,cols,n,c,r,g,b,p,obri,pbri |
||
33 | cols = #pal |
||
34 | bestcol = 0 |
||
35 | best = 9e99 |
||
36 | |||
37 | r = rgb[1] |
||
38 | g = rgb[2] |
||
39 | b = rgb[3] |
||
40 | |||
41 | obri = math.pow(r*9,2)+math.pow(g*16,2)+math.pow(b*8,2) |
||
42 | |||
43 | for n=0, cols-1, 1 do |
||
44 | p = pal[n+1] |
||
45 | pbri = math.pow(p[1]*9,2)+math.pow(p[2]*16,2)+math.pow(p[3]*8,2) |
||
46 | diffB = math.abs(obri - pbri) |
||
47 | |||
48 | |||
49 | diffC = (math.pow(r-p[1],2)+math.pow(g-p[2],2)+math.pow(b-p[3],2)) * 400 |
||
50 | --diff = diffB + diffC |
||
51 | diff = briweight * (diffB - diffC) + diffC |
||
52 | if diff <= best then bestcol = n; best = diff; end |
||
53 | end |
||
54 | |||
55 | return bestcol |
||
56 | end |
||
57 | -- |
||
58 | |||
59 | -- |
||
60 | function drawRectangle(x1,y1,w,h,c) |
||
61 | for y = y1, y1+h, 1 do |
||
62 | for x = x1, x1+w, 1 do |
||
63 | putbrushpixel(x,y,c); |
||
64 | end |
||
65 | end |
||
66 | end |
||
67 | -- |
||
68 | |||
69 | |||
70 | |||
71 | palList = makePalList(colors) |
||
72 | |||
73 | cf = getforecolor() |
||
74 | cb = getbackcolor() |
||
75 | rf,gf,bf = getcolor(cf) |
||
76 | rb,gb,bb = getcolor(cb) |
||
77 | |||
78 | ra = (rf + rb) / 2 |
||
79 | ga = (gf + gb) / 2 |
||
80 | ba = (bf + bb) / 2 |
||
81 | |||
82 | rgb1 = {ra,ga,ba} |
||
83 | c1 = getBestPalMatchHYBRID(rgb1,palList,0.0) |
||
84 | c2 = getBestPalMatchHYBRID(rgb1,palList,0.75) |
||
85 | c3 = getBestPalMatchHYBRID(rgb1,palList,0.99) |
||
86 | |||
87 | q = {{cf,c1,cb}, |
||
88 | {cf,c2,cb}, |
||
89 | {cf,c3,cb}} |
||
90 | |||
91 | |||
92 | for y = 0, #q-1, 1 do |
||
93 | for x = 0, #q[1]-1, 1 do |
||
94 | |||
95 | drawRectangle(x*cellw,y*cellh,cellw,cellh,q[y+1][x+1]) |
||
96 | |||
97 | end |
||
98 | end=> |
||
99 |