Debian Codegraph @

podofo/tools/podofocolor/example.lua

raw
1 -- /***************************************************************************
2 -- * Copyright (C) 2010 by Dominik Seichter *
3 -- * [email protected] *
4 -- * Stefan Huber *
5 -- * [email protected] *
6 -- * *
7 -- * This program is free software; you can redistribute it and/or modify *
8 -- * it under the terms of the GNU General Public License as published by *
9 -- * the Free Software Foundation; either version 2 of the License, or *
10 -- * (at your option) any later version. *
11 -- * *
12 -- * This program is distributed in the hope that it will be useful, *
13 -- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 -- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 -- * GNU General Public License for more details. *
16 -- * *
17 -- * You should have received a copy of the GNU General Public License *
18 -- * along with this program; if not, write to the *
19 -- * Free Software Foundation, Inc., *
20 -- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 -- ***************************************************************************/
22
23 function start_page (n)
24 print( " -> Lua is parsing page:", n + 1)
25 end
26
27 function end_page (n)
28 -- Do nothing
29 end
30
31 function start_xobjext ()
32 -- Do nothing
33 end
34
35 function end_xobject ()
36 -- Do nothing
37 end
38
39 -- This method is called whenever a gray stroking color is set
40 -- using the 'G' PDF command.
41 --
42 -- @param a grayscale color object
43 -- @returns a new color which should be set instead (any colorspace)
44 function set_stroking_color_gray (g)
45 -- Convert all gray values to RGB red
46 local a = { 1.0,0.0,0.0 }
47 return a
48 end
49
50 -- This method is called whenever a rgb stroking color is set
51 -- using the 'RG' PDF command.
52 --
53 -- @param a rgb color object
54 -- @returns a new color which should be set instead (any colorspace)
55 function set_stroking_color_rgb (r,g,b)
56 -- convert all black rgb values to cmyk,
57 -- leave other as they are
58 if r == 0 and
59 g == 0 and
60 b == 0 then
61 return { 0.0, 0.0, 0.0, 1.0 }
62 else
63 return { r,g,b }
64 end
65 end
66
67 -- This method is called whenever a cmyk stroking color is set
68 -- using the 'K' PDF command.
69 --
70 -- @param a cmyk color object
71 -- @returns a new color which should be set instead (any colorspace)
72 function set_stroking_color_cmyk (c,m,y,k)
73 -- do not change color,
74 -- just return input values again
75 return { c,m,y,k }
76 end
77
78 -- This method is called whenever a gray non-stroking color is set
79 -- using the 'g' PDF command.
80 --
81 -- @param a grayscale color object
82 -- @returns a new color which should be set instead (any colorspace)
83 function set_non_stroking_color_gray (g)
84 -- do not change color,
85 -- just return input values again
86 return { g }
87 end
88
89 -- This method is called whenever a rgb stroking color is set
90 -- using the 'rg' PDF command.
91 --
92 -- @param a rgb color object
93 -- @returns a new color which should be set instead (any colorspace)
94 function set_non_stroking_color_rgb (r,g,b)
95 -- Handle stroking and non-stroking rgb values in the same way
96 local c = set_stroking_color_rgb (r,g,b)
97 return c
98 end
99
100 -- This method is called whenever a cmyk non-stroking color is set
101 -- using the 'k' PDF command.
102 --
103 -- @param a cmyk color object
104 -- @returns a new color which should be set instead (any colorspace)
105 function set_non_stroking_color_cmyk (c,m,y,k)
106 -- do not change color,
107 -- just return input values again
108 return { c,m,y,k }
109 end
110
111 -- This method converts a given RGB color to CMYK
112 -- The conversion is like a maximal
113 -- under color removal
114 -- http://en.wikipedia.org/wiki/Under_color_removal
115 -- according to PoDoFo PdfColor::ConvertToCMYK()
116 --
117 -- @param a rgb color object
118 -- @returns the new CMYK color
119 function ConvertRGBtoCMYK(r,g,b)
120 -- local k = math.min( 1.0-r, math.min( 1.0-g, 1.0-b ) )
121 local k = math.min( 1.0-r, 1.0-g, 1.0-b )
122
123 local c = 0.0
124 local m = 0.0
125 local y = 0.0
126 if k < 1.0 then
127 c = (1.0 - r - k) / (1.0 - k)
128 m = (1.0 - g - k) / (1.0 - k)
129 y = (1.0 - b - k) / (1.0 - k)
130 end
131
132 return { c, m, y, k }
133 end
134
135 -- This method converts a given RGB color to Gray
136 -- according to PoDoFo PdfColor::ConvertToGrayScale()
137 --
138 -- @param a rgb color object
139 -- @returns the new GrayScale color
140 function ConvertRGBtoGrayScale(r,g,b)
141 return { 0.299*r + 0.587*g + 0.114*b }
142 end
143
144 -- Check if the given Color is nearly Gray
145 -- IsNearlyGray(0.126,0.127,0.128, 0.002) = true
146 -- IsNearlyGray(0.125,0.127,0.128, 0.002) = false
147 --
148 -- @param a rgb color object plus a tolerance-value
149 -- @returns true or false
150 function IsNearlyGray(r,g,b,t)
151 local min = math.min( r,g,b )
152 local max = math.max( r,g,b )
153
154 if max - min <= t and max - min >= -t then
155 return true
156 else
157 return false
158 end
159 end
160
161 -- Check if the given RGB Color1 is nearly RGB Color2
162 --
163 -- @param two rgb colors plus a tolerance-value
164 -- @returns true or false
165 function IsNearlyTheSameRGB(r1,g1,b1, r2,g2,b2, t)
166 local rMin = math.min( r1,r2 )
167 local rMax = math.max( r1,r2 )
168 local rSame = false
169
170 local gMin = math.min( g1,g2 )
171 local gMax = math.max( g1,g2 )
172 local gSame = false
173
174 local bMin = math.min( b1,b2 )
175 local bMax = math.max( b1,b2 )
176 local bSame = false
177
178 if rMax - rMin <= t and rMax - rMin >= -t then
179 rSame = true
180 end
181
182 if gMax - gMin <= t and gMax - gMin >= -t then
183 gSame = true
184 end
185
186 if bMax - bMin <= t and bMax - bMin >= -t then
187 bSame = true
188 end
189
190 if rSame and gSame and bSame then
191 return true
192 else
193 return false
194 end
195 end