Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6581 leency 1
--PALETTE: Expand Colors v1.0
2
--by Richard Fhager
3
--http://hem.fyristorg.com/dawnbringer/
4
-- Email: dawnbringer@hem.utfors.se
5
-- MSN:   annassar@hotmail.com
6
--
7
-- Copyright 2010 Richard Fhager
8
--
9
-- This program is free software; you can redistribute it and/or
10
-- modify it under the terms of the GNU General Public License
11
-- as published by the Free Software Foundation; version 2
12
-- of the License. See 
13
 
14
 
15
-- Continously fill the greatest void in the area of the color-cube enclosed by (or along ramps of) initial colors
16
-- This algorithm will create lines of allowed colors (all ranges) in 3d colorspace and the pick
17
-- new colors from the most void areas (on any line). Almost like a Median-cut in reverse.
18
--
19
-- Rather than filling the colorcube symmetrically it adds intermediate colors to the existing ones.
20
--
21
-- Running this script on the C64 16-color palette might be educational
22
--
23
--
24
--   Source cols#, Expand to #,
25
--   Ex: 15-31 means that palette colors 0-15 is expanded to 16 new colors placed at slots 16-31
26
--
27
--    Spread mode: OFF - New colors will conform to the contrast & saturation of original colors
28
--                       (new colors will stay on the ramps possible from the original colors)
29
--
30
--                 ON - New colors will expand their variance by each new addition (mostly notable when adding many new colors)
31
--                      Will add range lines/ramps to all new colors from old ones, but keep within max/min values of the
32
--                      original colors. 15-bit mode will dampen the spread towards extreme colors (if starting with low contrast)
33
--
34
--  15-bit colors: Higher color-resolution, 32768 possible colors rather than the 4096 of 12bit. Slower but perhaps better.
35
--
36
 
37
SHADES = 16 -- Going 24bit will probably be too slow and steal too much memory, so start with 12bit (4096 colors) for now
38
 
39
ini = 0
40
exp = 255
41
 
42
OK,ini,exp,linemode,fbit = inputbox("Expand Colors (0-255):",
43
                           "Source Cols #: 1-254",    15,  1,254,0,
44
                           "Expand to #: 2-255",       31,  2,255,0,
45
                           "Spread mode",       0,  0,1,0,
46
                           "15-bit colors (slow)", 0,  0,1,0
47
);
48
 
49
if (fbit == 1) then SHADES = 32; end
50
 
51
 
52
 
53
function initColorCube(sha)
54
  ary = {}
55
  for z = 0, sha-1, 1 do
56
   ary[z+1] = {}
57
   for y = 0, sha-1, 1 do
58
    ary[z+1][y+1] = {}
59
      for x = 0, sha-1, 1 do
60
        ary[z+1][y+1][x+1] = {false,0}
61
      end
62
   end
63
  end
64
  return ary
65
end
66
 
67
-- Gravity model (think of colors as stars of equal mass/brightness in a 3d space)
68
function addColor2Cube(cube,sha,r,g,b)
69
  star = 1000000
70
  fade = 1000
71
 
72
  cube[r+1][g+1][b+1] = {false,star}
73
 
74
  for z = 0, sha-1, 1 do
75
   for y = 0, sha-1, 1 do
76
    for x = 0, sha-1, 1 do
77
 
78
      d = fade / ( (x-b)^2 + (y-g)^2 + (z-r)^2 )
79
 
80
      cube[z+1][y+1][x+1][2] = cube[z+1][y+1][x+1][2] + d
81
 
82
  end;end;end
83
end
84
 
85
 
86
-- Create new allowed colorlines in colorspace (ramps from which colors can be picked)
87
function enableRangeColorsInCube(cube,sha,r1,g1,b1,r2,g2,b2)
88
 
89
    local div,r,g,b
90
    div = 256 / sha
91
    rs = (r2 - r1) / sha / div
92
    gs = (g2 - g1) / sha / div
93
    bs = (b2 - b1) / sha / div
94
 
95
    for n = 0, sha-1, 1 do
96
 
97
     r = math.floor(r1/div + rs * n)
98
     g = math.floor(g1/div + gs * n)
99
     b = math.floor(b1/div + bs * n)
100
 
101
     cube[r+1][g+1][b+1][1] = true
102
 
103
    end
104
end
105
 
106
 
107
function findVoid(cube,sha)
108
  weakest = 999999999999
109
   weak_i = {-1,-1,-1}
110
  for z = 0, sha-1, 1 do
111
   for y = 0, sha-1, 1 do
112
    for x = 0, sha-1, 1 do
113
 
114
      c = cube[z+1][y+1][x+1]
115
      if c[1] == true then
116
        w = c[2]
117
        if w <= weakest then weakest = w; weak_i = {z,y,x}; end
118
      end
119
 
120
  end;end;end
121
  return weak_i[1],weak_i[2],weak_i[3]
122
end
123
 
124
--
125
 
126
if OK == true then
127
 
128
  cube = initColorCube(SHADES)
129
 
130
  -- Define allowed colorspace
131
  for y = 0, ini-1, 1 do
132
    r1,g1,b1 = getcolor(y)
133
    for x = y+1, ini, 1 do
134
      r2,g2,b2 = getcolor(x)
135
      enableRangeColorsInCube(cube,SHADES,r1,g1,b1,r2,g2,b2)
136
    end
137
  end
138
 
139
  div = 256 / SHADES
140
 
141
  -- Fill cube with initial colors
142
  for n = 0, ini, 1 do
143
    r,g,b = getcolor(n)
144
    addColor2Cube(cube,SHADES,math.floor(r/div),math.floor(g/div),math.floor(b/div))
145
  end
146
 
147
 
148
  for n = ini+1, exp, 1 do
149
    r,g,b = findVoid(cube,SHADES)
150
 
151
    if (r == -1) then messagebox("Report:","No more colors can be found, exit at "..n); break; end
152
 
153
    mult = 255 / (SHADES - 1)
154
    setcolor(n, r*mult,g*mult,b*mult)
155
 
156
    if linemode == 1 then
157
       -- Add lines from new color to all old
158
       for x = 0, n-1, 1 do
159
          r2,g2,b2 = getcolor(x)
160
          enableRangeColorsInCube(cube,SHADES,r*mult,g*mult,b*mult,r2,g2,b2) -- uses 24bit values rgb
161
       end
162
    end
163
 
164
    addColor2Cube(cube,SHADES,r,g,b) -- rgb is in 'shade' format here
165
 
166
  end
167
 
168
end
169