-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFU_color_color-temp.scm
463 lines (425 loc) · 18 KB
/
FU_color_color-temp.scm
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
; FU_color_color-temp.scm
; version 2.9 [gimphelp.org]
; last modified/tested by Paul Sherman
; 02/15/2014 on GIMP-2.8.10
; 04/26/2020 on GIMP-2.10.18 by karlhof26
;
;==============================================================
;
; Installation:
; This script should be placed in the user or system-wide script folder.
;
; Windows 10
; C:\Program Files\GIMP 2\share\gimp\2.0\scripts
; or
; C:\Users\YOUR-NAME\.gimp-2.10\scripts
;
;
; Linux
; /home/yourname/.gimp-2.8/scripts
; or
; Linux system-wide
; /usr/share/gimp/2.0/scripts
;
;==============================================================
;
; LICENSE
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program 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 General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
;
;==============================================================
; Original information
;
; NOT AVAILABLE
; Improved by karlhof26 for Gimp 2.10.32
;==============================================================
(define (FU-colortemp
image
drawable
sourcemod
sourcetemp
desttemp
intensmult
satura
)
(gimp-image-undo-group-start image)
(if (not (= RGB (car (gimp-image-base-type image))))
(gimp-image-convert-rgb image)
)
(define (floor x) (- x (fmod x 1)))
; Linear interpolations, used by the table lookup below
(define (interpolate xlow xhi ylow yhi x)
(let* (
(deltax (- xhi xlow))
(deltap (- x xlow))
(deltay (- yhi ylow))
(inc 0)
)
;(gimp-message "interpolate==================================")
;(gimp-message (number->string deltap))
;(gimp-message (number->string deltay))
;(gimp-message (number->string deltax))
;(set! deltax (max 0.0001 deltax))
(set! inc (* deltay (/ deltap deltax)))
(+ ylow inc)
)
)
; Interpolates colors
(define (interpolate-rows low high temp)
(let* (
(t_low (car low))
(r_low (cadr low))
(g_low (caddr low))
(b_low (nth 3 low))
(t_hi (car high))
(r_hi (cadr high))
(g_hi (caddr high))
(b_hi (nth 3 high))
)
(list
(interpolate t_low t_hi r_low r_hi temp)
(interpolate t_low t_hi g_low g_hi temp)
(interpolate t_low t_hi b_low b_hi temp)
)
)
)
; Given the colortable and color temperature in K, finds
; r, g, b values in the linear space.
(define (findcolors colortableK tempK)
(let* (
(lp colortableK)
(lowK (car colortableK))
)
;(gimp-message "line114")
;(gimp-message (number->string tempK))
;(gimp-message (number->string (car lowK)))
;(gimp-message (number->string (car (car lp))))
(while (> tempK (car (car lp)))
(set! lowK (car lp))
(set! lp (cdr lp))
;(gimp-message (number->string tempK))
;(gimp-message (number->string (car lowK)))
;(gimp-message (number->string (car (car lp))))
;(gimp-message "line117 loop")
(gimp-progress-update (/ (car (car lp)) tempK))
)
;(gimp-message "line123")
(interpolate-rows lowK (car lp) tempK)
)
)
; Converts from linear to gamma-corrected sRGB [0,1]
(define (togamma x)
(if (<= x 0.00304)
(* x 12.92)
(let* ((exponent (/ 1 2.4)))
(- (* 1.055 (pow x exponent)) 0.055)
)
)
)
; Converts from linear to gamma-corrected sRGB [0,255]
(define (togamma255 x)
(max (min (floor (* 256 (togamma x))) 255) 0)
)
; Converts from gamma-corrected sRGB [0,1] to linear
(define (tolinear y)
(if (<= y 0.0392768)
(/ y 12.92)
(let* (
(ratio (/ (+ y 0.055) 1.055))
)
(pow ratio 2.4)
)
)
)
; Converts from gamma-corrected sRGB [0,255] to linear
(define (to255linear y)
(tolinear (/ y 255))
)
; Applies a ratio (in linear space) to sRGB values, where the sRGB
; values are scaled 0..255.
(define (lin-mult-in-gamma-space y255 r)
(togamma255 (* r (to255linear y255)))
)
; Finds the colors (in linear space) of the temperature whose color
; best matches the foreground. It uses a least-square fit (any better ideas?).
(define (sourcecolors-from-bestmatch colortable fg)
(let* (
(fg-red (to255linear (car fg)))
(fg-green (to255linear (cadr fg)))
(fg-blue (to255linear (caddr fg)))
(mm1 0.8)
)
;(gimp-message "line161")
;(gimp-message (number->string fg-red))
;(gimp-message (number->string fg-green))
;(gimp-message (number->string fg-blue))
(set! mm1 (max fg-red (max fg-green fg-blue)))
;(gimp-message (number->string mm1))
(if (< mm1 0.0001) ; was 0.000001
(begin
(list 1 1 1)
)
(begin
;(gimp-message "line167")
(let* (
(nr (min 1 (/ fg-red mm1)))
(ng (min 1 (/ fg-green mm1)))
(nb (min 1 (/ fg-blue mm1)))
(t 1050) ; must be more than the lowest entry in the color table which is 1000; we had divide by 0 error
(d 4)
(best (list 1 1 1))
)
;(gimp-message "line175")
(gimp-progress-init "Searching for best values (slow)" -1)
(while (<= t 12000)
(let* (
(c (findcolors colortable t))
(cr (car c))
(cg (cadr c))
(cb (caddr c))
(dr (- cr nr))
(dg (- cg ng))
(db (- cb nb))
(d1 (+ (* dr dr) (+ (* dg dg) (* db db))))
)
(if (< d1 d)
(begin
(gimp-progress-pulse)
;(gimp-message "line197 ******************************************")
(set! best c)
)
)
(if (< d1 d)
(begin
(set! d d1)
)
)
)
;(gimp-message (number->string d1))
(gimp-progress-update (/ t 12000))
(set! t (+ t 50)) ; we compute the temp within 50K; should be enough
)
;(gimp-message "line194")
best ;return
)
)
)
)
)
; Finds the colors (in linear space) of the foreground.
(define (sourcecolors-from-fg fg)
(let* (
(fg-red (to255linear (car fg)))
(fg-green (to255linear (cadr fg)))
(fg-blue (to255linear (caddr fg)))
(mm2 (max fg-red (max fg-green fg-blue)))
)
(gimp-message (number->string mm2))
(if (< mm2 0.000001)
(list 1 1 1)
(begin
(gimp-message "line217")
(let* (
(nr (min 1 (/ fg-red mm2)))
(ng (min 1 (/ fg-green mm2)))
(nb (min 1 (/ fg-blue mm2)))
)
(list nr ng nb)
)
)
)
)
)
; This is the color table, taken from
; http://www.vendian.org/mncharity/dir3/blackbody/UnstableURLs/bbr_color.html
(let* (
(colortemp
(list
(list 1000 1.0000 0.0401 0.0000)
(list 1100 1.0000 0.0631 0.0000)
(list 1200 1.0000 0.0860 0.0000)
(list 1300 1.0000 0.1085 0.0000)
(list 1400 1.0000 0.1303 0.0000)
(list 1500 1.0000 0.1515 0.0000)
(list 1600 1.0000 0.1718 0.0000)
(list 1700 1.0000 0.1912 0.0000)
(list 1800 1.0000 0.2097 0.0000)
(list 1900 1.0000 0.2272 0.0000)
(list 2000 1.0000 0.2484 0.0061)
(list 2100 1.0000 0.2709 0.0153)
(list 2200 1.0000 0.2930 0.0257)
(list 2300 1.0000 0.3149 0.0373)
(list 2400 1.0000 0.3364 0.0501)
(list 2500 1.0000 0.3577 0.0640)
(list 2600 1.0000 0.3786 0.0790)
(list 2700 1.0000 0.3992 0.0950)
(list 2800 1.0000 0.4195 0.1119)
(list 2900 1.0000 0.4394 0.1297)
(list 3000 1.0000 0.4589 0.1483)
(list 3100 1.0000 0.4781 0.1677)
(list 3200 1.0000 0.4970 0.1879)
(list 3300 1.0000 0.5155 0.2087)
(list 3400 1.0000 0.5336 0.2301)
(list 3500 1.0000 0.5515 0.2520)
(list 3600 1.0000 0.5689 0.2745)
(list 3700 1.0000 0.5860 0.2974)
(list 3800 1.0000 0.6028 0.3207)
(list 3900 1.0000 0.6193 0.3444)
(list 4000 1.0000 0.6354 0.3684)
(list 4100 1.0000 0.6511 0.3927)
(list 4200 1.0000 0.6666 0.4172)
(list 4300 1.0000 0.6817 0.4419)
(list 4400 1.0000 0.6966 0.4668)
(list 4500 1.0000 0.7111 0.4919)
(list 4600 1.0000 0.7253 0.5170)
(list 4700 1.0000 0.7392 0.5422)
(list 4800 1.0000 0.7528 0.5675)
(list 4900 1.0000 0.7661 0.5928)
(list 5000 1.0000 0.7792 0.6180)
(list 5100 1.0000 0.7919 0.6433)
(list 5200 1.0000 0.8044 0.6685)
(list 5300 1.0000 0.8167 0.6937)
(list 5400 1.0000 0.8286 0.7187)
(list 5500 1.0000 0.8403 0.7437)
(list 5600 1.0000 0.8518 0.7686)
(list 5700 1.0000 0.8630 0.7933)
(list 5800 1.0000 0.8740 0.8179)
(list 5900 1.0000 0.8847 0.8424)
(list 6000 1.0000 0.8952 0.8666)
(list 6100 1.0000 0.9055 0.8907)
(list 6200 1.0000 0.9156 0.9147)
(list 6300 1.0000 0.9254 0.9384)
(list 6400 1.0000 0.9351 0.9619)
(list 6500 1.0000 0.9445 0.9853)
(list 6600 0.9917 0.9458 1.0000)
(list 6700 0.9696 0.9336 1.0000)
(list 6800 0.9488 0.9219 1.0000)
(list 6900 0.9290 0.9107 1.0000)
(list 7000 0.9102 0.9000 1.0000)
(list 7100 0.8923 0.8897 1.0000)
(list 7200 0.8753 0.8799 1.0000)
(list 7300 0.8591 0.8704 1.0000)
(list 7400 0.8437 0.8614 1.0000)
(list 7500 0.8289 0.8527 1.0000)
(list 7600 0.8149 0.8443 1.0000)
(list 7700 0.8014 0.8363 1.0000)
(list 7800 0.7885 0.8285 1.0000)
(list 7900 0.7762 0.8211 1.0000)
(list 8000 0.7644 0.8139 1.0000)
(list 8100 0.7531 0.8069 1.0000)
(list 8200 0.7423 0.8002 1.0000)
(list 8300 0.7319 0.7938 1.0000)
(list 8400 0.7219 0.7875 1.0000)
(list 8500 0.7123 0.7815 1.0000)
(list 8600 0.7030 0.7757 1.0000)
(list 8700 0.6941 0.7700 1.0000)
(list 8800 0.6856 0.7645 1.0000)
(list 8900 0.6773 0.7593 1.0000)
(list 9000 0.6693 0.7541 1.0000)
(list 9100 0.6617 0.7492 1.0000)
(list 9200 0.6543 0.7444 1.0000)
(list 9300 0.6471 0.7397 1.0000)
(list 9400 0.6402 0.7352 1.0000)
(list 9500 0.6335 0.7308 1.0000)
(list 9600 0.6271 0.7265 1.0000)
(list 9700 0.6208 0.7224 1.0000)
(list 9800 0.6148 0.7183 1.0000)
(list 9900 0.6089 0.7144 1.0000)
(list 10000 0.6033 0.7106 1.0000)
(list 10100 0.5978 0.7069 1.0000)
(list 10200 0.5925 0.7033 1.0000)
(list 10300 0.5873 0.6998 1.0000)
(list 10400 0.5823 0.6964 1.0000)
(list 10500 0.5774 0.6930 1.0000)
(list 10600 0.5727 0.6898 1.0000)
(list 10700 0.5681 0.6866 1.0000)
(list 10800 0.5637 0.6836 1.0000)
(list 10900 0.5593 0.6806 1.0000)
(list 11000 0.5551 0.6776 1.0000)
(list 11100 0.5510 0.6748 1.0000)
(list 11200 0.5470 0.6720 1.0000)
(list 11300 0.5432 0.6693 1.0000)
(list 11400 0.5394 0.6666 1.0000)
(list 11500 0.5357 0.6640 1.0000)
(list 11600 0.5322 0.6615 1.0000)
(list 11700 0.5287 0.6590 1.0000)
(list 11800 0.5253 0.6566 1.0000)
(list 11900 0.5220 0.6542 1.0000)
(list 12000 0.5187 0.6519 1.0000)
)
)
; Foreground
(fg (car (gimp-context-get-foreground)))
; Finds the linear source colors
; The colors are taken from slider temperature
(sourcecolors (cond ((= sourcemod 0) (findcolors colortemp sourcetemp))
; The colors are taken from foreground best-match
((= sourcemod 1) (sourcecolors-from-bestmatch colortemp fg)))
)
; and the target colors
(targetcolors (findcolors colortemp desttemp))
; computes the ratios
(m 0.1)
(rr (/ (car targetcolors) (car sourcecolors)))
(rg (/ (cadr targetcolors) (cadr sourcecolors)))
(rb (/ (caddr targetcolors) (caddr sourcecolors)))
; Multiplies them by the intensity modification
; m calc was here
; And these are the real ratios
(rratio 0)
(gratio 1)
(bratio 2)
(i 0)
(num_bytes 256)
(red-curve (cons-array num_bytes 'byte))
(green-curve (cons-array num_bytes 'byte))
(blue-curve (cons-array num_bytes 'byte))
)
;(gimp-message "ok start")
(set! m (/ intensmult 100))
(set! rratio (* rr m))
(set! gratio (* rg m))
(set! bratio (* rb m))
(while (< i num_bytes)
(aset red-curve i (lin-mult-in-gamma-space i rratio))
(aset green-curve i (lin-mult-in-gamma-space i gratio))
(aset blue-curve i (lin-mult-in-gamma-space i bratio))
(set! i (+ i 1))
)
(gimp-curves-explicit drawable HISTOGRAM-RED num_bytes red-curve )
(gimp-curves-explicit drawable HISTOGRAM-GREEN num_bytes green-curve)
(gimp-curves-explicit drawable HISTOGRAM-BLUE num_bytes blue-curve )
;(gimp-hue-saturation drawable 0 0.0 0.0 satura)
(gimp-drawable-hue-saturation drawable HUE-RANGE-ALL 0.0 0.0 satura 0.0)
(gimp-displays-flush)
(gimp-image-undo-group-end image)
(gc) ; garbage clean-up
)
)
(script-fu-register "FU-colortemp"
"<Toolbox>/Script-Fu/Colors/Color Temp"
"This script-fu converts the color temperature of an image. You can specify the color temperature of the original image. Foreground color option is not recommended as the search is very slow. \n file:FU_color_color-temp.scm"
"Luca de Alfaro <lda@dealfaro.com>"
"Luca de Alfaro"
"December 2006"
"*"
SF-IMAGE "Image" 0
SF-DRAWABLE "Drawable" 0
SF-OPTION "Obtain original temperature" '("From slider below" "From foreground color")
SF-ADJUSTMENT "Original temperature (K)" '(6500 1000 12000 25 250 0 0)
SF-ADJUSTMENT "Target temperature (K)" '(6500 1000 12000 25 250 0 0)
SF-ADJUSTMENT "Intensity (%)" '(100 0 200 1 10 0 0)
SF-ADJUSTMENT "Saturation change (%)" '(0 -100 100 1 10 0 0)
)
;end of script