/ chmod777_includes / html_css / css_color.lua
css_color.lua
  1  -- File: css_color.lua
  2  
  3  --[[
  4  Copyright (C) 2024 chmod777
  5  
  6  This program is free software: you can redistribute it and/or modify it under
  7  the terms of the GNU Affero General Public License version 3 as published by the
  8  Free Software Foundation.
  9  
 10  This program is distributed in the hope that it will be useful, but WITHOUT ANY
 11  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 12  PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
 13  
 14  You should have received a copy of the GNU Affero General Public License along
 15  with this program. If not, see <https://www.gnu.org/licenses/>. 
 16  ]]
 17  
 18  
 19  Color = {
 20  	['transparent'] = function() return Color:new(0, 0, 0, 0) end,
 21  	
 22  	['aliceblue'] = function() return Color:new(240, 248, 255) end,
 23  	['antiquewhite'] = function() return Color:new(250, 235, 215) end,
 24  	['aqua'] = function() return Color:new(0, 255, 255) end,
 25  	['aquamarine'] = function() return Color:new(127, 255, 212) end,
 26  	['azure'] = function() return Color:new(240, 255, 255) end,
 27  	['beige'] = function() return Color:new(245, 245, 220) end,
 28  	['bisque'] = function() return Color:new(255, 228, 196) end,
 29  	['black'] = function() return Color:new(0, 0, 0) end,
 30  	['blanchedalmond'] = function() return Color:new(255, 235, 205) end,
 31  	['blue'] = function() return Color:new(0, 0, 255) end,
 32  	['blueviolet'] = function() return Color:new(138, 43, 226) end,
 33  	['brown'] = function() return Color:new(165, 42, 42) end,
 34  	['burlywood'] = function() return Color:new(222, 184, 135) end,
 35  	['cadetblue'] = function() return Color:new(95, 158, 160) end,
 36  	['chartreuse'] = function() return Color:new(127, 255, 0) end,
 37  	['chocolate'] = function() return Color:new(210, 105, 30) end,
 38  	['coral'] = function() return Color:new(255, 127, 80) end,
 39  	['cornflowerblue'] = function() return Color:new(100, 149, 237) end,
 40  	['cornsilk'] = function() return Color:new(255, 248, 220) end,
 41  	['crimson'] = function() return Color:new(220, 20, 60) end,
 42  	['cyan'] = function() return Color:new(0, 255, 255) end,
 43  	['darkblue'] = function() return Color:new(0, 0, 139) end,
 44  	['darkcyan'] = function() return Color:new(0, 139, 139) end,
 45  	['darkgoldenrod'] = function() return Color:new(184, 134, 11) end,
 46  	['darkgray'] = function() return Color:new(169, 169, 169) end,
 47  	['darkgrey'] = function() return Color:new(169, 169, 169) end,
 48  	['darkgreen'] = function() return Color:new(0, 100, 0) end,
 49  	['darkkhaki'] = function() return Color:new(189, 183, 107) end,
 50  	['darkmagenta'] = function() return Color:new(139, 0, 139) end,
 51  	['darkolivegreen'] = function() return Color:new(85, 107, 47) end,
 52  	['darkorange'] = function() return Color:new(255, 140, 0) end,
 53  	['darkorchid'] = function() return Color:new(153, 50, 204) end,
 54  	['darkred'] = function() return Color:new(139, 0, 0) end,
 55  	['darksalmon'] = function() return Color:new(233, 150, 122) end,
 56  	['darkseagreen'] = function() return Color:new(143, 188, 143) end,
 57  	['darkslateblue'] = function() return Color:new(72, 61, 139) end,
 58  	['darkslategray'] = function() return Color:new(47, 79, 79) end,
 59  	['darkslategrey'] = function() return Color:new(47, 79, 79) end,
 60  	['darkturquoise'] = function() return Color:new(0, 206, 209) end,
 61  	['darkviolet'] = function() return Color:new(148, 0, 211) end,
 62  	['deeppink'] = function() return Color:new(255, 20, 147) end,
 63  	['deepskyblue'] = function() return Color:new(0, 191, 255) end,
 64  	['dimgray'] = function() return Color:new(105, 105, 105) end,
 65  	['dimgrey'] = function() return Color:new(105, 105, 105) end,
 66  	['dodgerblue'] = function() return Color:new(30, 144, 255) end,
 67  	['firebrick'] = function() return Color:new(178, 34, 34) end,
 68  	['floralwhite'] = function() return Color:new(255, 250, 240) end,
 69  	['forestgreen'] = function() return Color:new(34, 139, 34) end,
 70  	['fuchsia'] = function() return Color:new(255, 0, 255) end,
 71  	['gainsboro'] = function() return Color:new(220, 220, 220) end,
 72  	['ghostwhite'] = function() return Color:new(248, 248, 255) end,
 73  	['gold'] = function() return Color:new(255, 215, 0) end,
 74  	['goldenrod'] = function() return Color:new(218, 165, 32) end,
 75  	['gray'] = function() return Color:new(128, 128, 128) end,
 76  	['grey'] = function() return Color:new(128, 128, 128) end,
 77  	['green'] = function() return Color:new(0, 128, 0) end,
 78  	['greenyellow'] = function() return Color:new(173, 255, 47) end,
 79  	['honeydew'] = function() return Color:new(240, 255, 240) end,
 80  	['hotpink'] = function() return Color:new(255, 105, 180) end,
 81  	['indianred'] = function() return Color:new(205, 92, 92) end,
 82  	['indigo'] = function() return Color:new(75, 0, 130) end,
 83  	['ivory'] = function() return Color:new(255, 255, 240) end,
 84  	['khaki'] = function() return Color:new(240, 230, 140) end,
 85  	['lavender'] = function() return Color:new(230, 230, 250) end,
 86  	['lavenderblush'] = function() return Color:new(255, 240, 245) end,
 87  	['lawngreen'] = function() return Color:new(124, 252, 0) end,
 88  	['lemonchiffon'] = function() return Color:new(255, 250, 205) end,
 89  	['lightblue'] = function() return Color:new(173, 216, 230) end,
 90  	['lightcoral'] = function() return Color:new(240, 128, 128) end,
 91  	['lightcyan'] = function() return Color:new(224, 255, 255) end,
 92  	['lightgoldenrodyellow'] = function() return Color:new(250, 250, 210) end,
 93  	['lightgray'] = function() return Color:new(211, 211, 211) end,
 94  	['lightgrey'] = function() return Color:new(211, 211, 211) end,
 95  	['lightgreen'] = function() return Color:new(144, 238, 144) end,
 96  	['lightpink'] = function() return Color:new(255, 182, 193) end,
 97  	['lightsalmon'] = function() return Color:new(255, 160, 122) end,
 98  	['lightseagreen'] = function() return Color:new(32, 178, 170) end,
 99  	['lightskyblue'] = function() return Color:new(135, 206, 250) end,
100  	['lightslategray'] = function() return Color:new(119, 136, 153) end,
101  	['lightslategrey'] = function() return Color:new(119, 136, 153) end,
102  	['lightsteelblue'] = function() return Color:new(176, 196, 222) end,
103  	['lightyellow'] = function() return Color:new(255, 255, 224) end,
104  	['lime'] = function() return Color:new(0, 255, 0) end,
105  	['limegreen'] = function() return Color:new(50, 205, 50) end,
106  	['linen'] = function() return Color:new(250, 240, 230) end,
107  	['magenta'] = function() return Color:new(255, 0, 255) end,
108  	['maroon'] = function() return Color:new(128, 0, 0) end,
109  	['mediumaquamarine'] = function() return Color:new(102, 205, 170) end,
110  	['mediumblue'] = function() return Color:new(0, 0, 205) end,
111  	['mediumorchid'] = function() return Color:new(186, 85, 211) end,
112  	['mediumpurple'] = function() return Color:new(147, 112, 219) end,
113  	['mediumseagreen'] = function() return Color:new(60, 179, 113) end,
114  	['mediumslateblue'] = function() return Color:new(123, 104, 238) end,
115  	['mediumspringgreen'] = function() return Color:new(0, 250, 154) end,
116  	['mediumturquoise'] = function() return Color:new(72, 209, 204) end,
117  	['mediumvioletred'] = function() return Color:new(199, 21, 133) end,
118  	['midnightblue'] = function() return Color:new(25, 25, 112) end,
119  	['mintcream'] = function() return Color:new(245, 255, 250) end,
120  	['mistyrose'] = function() return Color:new(255, 228, 225) end,
121  	['moccasin'] = function() return Color:new(255, 228, 181) end,
122  	['navajowhite'] = function() return Color:new(255, 222, 173) end,
123  	['navy'] = function() return Color:new(0, 0, 128) end,
124  	['oldlace'] = function() return Color:new(253, 245, 230) end,
125  	['olive'] = function() return Color:new(128, 128, 0) end,
126  	['olivedrab'] = function() return Color:new(107, 142, 35) end,
127  	['orange'] = function() return Color:new(255, 165, 0) end,
128  	['orangered'] = function() return Color:new(255, 69, 0) end,
129  	['orchid'] = function() return Color:new(218, 112, 214) end,
130  	['palegoldenrod'] = function() return Color:new(238, 232, 170) end,
131  	['palegreen'] = function() return Color:new(152, 251, 152) end,
132  	['paleturquoise'] = function() return Color:new(175, 238, 238) end,
133  	['palevioletred'] = function() return Color:new(219, 112, 147) end,
134  	['papayawhip'] = function() return Color:new(255, 239, 213) end,
135  	['peachpuff'] = function() return Color:new(255, 218, 185) end,
136  	['peru'] = function() return Color:new(205, 133, 63) end,
137  	['pink'] = function() return Color:new(255, 192, 203) end,
138  	['plum'] = function() return Color:new(221, 160, 221) end,
139  	['powderblue'] = function() return Color:new(176, 224, 230) end,
140  	['purple'] = function() return Color:new(128, 0, 128) end,
141  	['rebeccapurple'] = function() return Color:new(102, 51, 153) end,
142  	['red'] = function() return Color:new(255, 0, 0) end,
143  	['rosybrown'] = function() return Color:new(188, 143, 143) end,
144  	['royalblue'] = function() return Color:new(65, 105, 225) end,
145  	['saddlebrown'] = function() return Color:new(139, 69, 19) end,
146  	['salmon'] = function() return Color:new(250, 128, 114) end,
147  	['sandybrown'] = function() return Color:new(244, 164, 96) end,
148  	['seagreen'] = function() return Color:new(46, 139, 87) end,
149  	['seashell'] = function() return Color:new(255, 245, 238) end,
150  	['sienna'] = function() return Color:new(160, 82, 45) end,
151  	['silver'] = function() return Color:new(192, 192, 192) end,
152  	['skyblue'] = function() return Color:new(135, 206, 235) end,
153  	['slateblue'] = function() return Color:new(106, 90, 205) end,
154  	['slategray'] = function() return Color:new(112, 128, 144) end,
155  	['slategrey'] = function() return Color:new(112, 128, 144) end,
156  	['snow'] = function() return Color:new(255, 250, 250) end,
157  	['springgreen'] = function() return Color:new(0, 255, 127) end,
158  	['steelblue'] = function() return Color:new(70, 130, 180) end,
159  	['tan'] = function() return Color:new(210, 180, 140) end,
160  	['teal'] = function() return Color:new(0, 128, 128) end,
161  	['thistle'] = function() return Color:new(216, 191, 216) end,
162  	['tomato'] = function() return Color:new(255, 99, 71) end,
163  	['turquoise'] = function() return Color:new(64, 224, 208) end,
164  	['violet'] = function() return Color:new(238, 130, 238) end,
165  	['wheat'] = function() return Color:new(245, 222, 179) end,
166  	['white'] = function() return Color:new(255, 255, 255) end,
167  	['whitesmoke'] = function() return Color:new(245, 245, 245) end,
168  	['yellow'] = function() return Color:new(255, 255, 0) end,
169  	['yellowgreen'] = function() return Color:new(154, 205, 50) end,
170  
171  	parse_color = function(parser)
172  		local source = parser.source
173  		local position = source.position
174  
175  		local ident = source:match_ident()
176  		if Color[ident] then
177  			return Color[ident]().remap_range()
178  		end
179  
180  		if ident ~= "rgb" and ident ~= "rgba" then
181  			source:seek(position)
182  			return nil
183  		end
184  	
185  		local start = source:match("%(")
186  		source:match_whitespaces()
187  		local r = source:match_number()
188  		source:match_whitespaces()
189  		local g = source:match_number()
190  		source:match_whitespaces()
191  		local b = source:match_number()
192  		source:match_whitespaces()
193  		if ident == "rgba" then
194  			local a = source:match_number()
195  			source:match_whitespaces()
196  		end
197  		local finished = source:match("%)")
198  	
199  		if start == nil or r == nil or g == nil or b == nil or finished == nil then
200  			source:seek(position)
201  			return nil
202  		end
203  	
204  		if ident == "rgba" and a == nil then
205  			source:seek(position)
206  			return nil
207  		end
208  	
209  		if a == nil then
210  			a = 255
211  		end
212  	
213  		local color = Color:new(r, g, b, a, 255)
214  		return color:remap_range(1)
215  	end,
216  }
217  function Color:new(r, g, b, a, range)
218  	if r == nil then r = 0 end
219  	if g == nil then g = 0 end
220  	if b == nil then b = 0 end
221  	if a == nil then a = 255 end
222  	if range == nil then range = 255 end
223  
224  	local this = {
225  		valueType = "color",
226  		range = range,
227  		value = {
228  			r = r,
229  			g = g,
230  			b = b,
231  			a = a,
232  		}
233  	}
234  
235  	function this:remap_range(new_range)
236  		if new_range == nil then new_range = 1 end
237  		local value = this.value
238  		local scale = new_range/this.range
239  		local r = scale * value.r
240  		local g = scale * value.g
241  		local b = scale * value.b
242  		local a = scale * value.a
243  		return Color:new(r, g, b, a, new_range)
244  	end
245  
246  	return this
247  end
248  
249  return Color