Debian Codegraph @

stylesheets/_blend-modes.scss

raw
1 // Import the Compass Plugin
2 //--------------------------------
3 // Normal
4 //--------------------------------
5 @function blend-normal ($foreground, $background) {
6 $opacity: opacity($foreground);
7 $background-opacity: opacity($background);
8
9 // calculate opacity
10 $bm-red: red($foreground) * $opacity + red($background) * $background-opacity * (1 - $opacity);
11 $bm-green: green($foreground) * $opacity + green($background) * $background-opacity * (1 - $opacity);
12 $bm-blue: blue($foreground) * $opacity + blue($background) * $background-opacity * (1 - $opacity);
13 @return rgb($bm-red, $bm-green, $bm-blue);
14 }
15
16 //--------------------------------
17 // Multiply
18 //--------------------------------
19 @function blend-multiply ($foreground, $background) {
20 $bm-red: red($background) * red($foreground) / 255;
21 $bm-green: green($background) * green($foreground) / 255;
22 $bm-blue: blue($background) * blue($foreground) / 255;
23
24 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
25 }
26
27 //--------------------------------
28 // Lighten
29 //--------------------------------
30 @function blend-lighten ($foreground, $background) {
31 $bm-red: blend-lighten-color(red($foreground), red($background));
32 $bm-green: blend-lighten-color(green($foreground), green($background));
33 $bm-blue: blend-lighten-color(blue($foreground), blue($background));
34
35 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
36 }
37 @function blend-lighten-color($foreground, $background) {
38 @if $background > $foreground {
39 $foreground: $background;
40 }
41 @return $foreground;
42 }
43
44 //--------------------------------
45 // Darken
46 //--------------------------------
47 @function blend-darken ($foreground, $background) {
48 $bm-red: blend-darken-color(red($foreground), red($background));
49 $bm-green: blend-darken-color(green($foreground), green($background));
50 $bm-blue: blend-darken-color(blue($foreground), blue($background));
51
52 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
53 }
54 @function blend-darken-color($foreground, $background) {
55 @if $background < $foreground {
56 $foreground: $background;
57 }
58 @return $foreground;
59 }
60
61 //--------------------------------
62 // Darker Color
63 //--------------------------------
64 @function blend-darkercolor ($foreground, $background) {
65 $bm-red: red($foreground);
66 $bm-green: green($foreground);
67 $bm-blue: blue($foreground);
68 $background-red: red($background);
69 $background-green: green($background);
70 $background-blue: blue($background);
71
72 @if $background-red * 0.3 + $background-green * 0.59 + $background-blue * 0.11 <= $bm-red * 0.3 + $bm-green * 0.59 + $bm-blue * 0.11 {
73 $bm-red: $background-red;
74 $bm-green: $background-green;
75 $bm-blue: $background-blue;
76 }
77 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
78 }
79
80 //--------------------------------
81 // Lighter Color
82 //--------------------------------
83 @function blend-lightercolor ($foreground, $background) {
84 $bm-red: red($foreground);
85 $bm-green: green($foreground);
86 $bm-blue: blue($foreground);
87 $background-red: red($background);
88 $background-green: green($background);
89 $background-blue: blue($background);
90
91 @if $background-red * 0.3 + $background-green * 0.59 + $background-blue * 0.11 > $bm-red * 0.3 + $bm-green * 0.59 + $bm-blue * 0.11 {
92 $bm-red: $background-red;
93 $bm-green: $background-green;
94 $bm-blue: $background-blue;
95 }
96 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
97 }
98
99 //--------------------------------
100 // Linear Dodge
101 //--------------------------------
102 @function blend-lineardodge ($foreground, $background) {
103 $bm-red: blend-lineardodge-color(red($foreground), red($background));
104 $bm-green: blend-lineardodge-color(green($foreground), green($background));
105 $bm-blue: blend-lineardodge-color(blue($foreground), blue($background));
106
107 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
108 }
109 @function blend-lineardodge-color($foreground, $background) {
110 @if $background + $foreground > 255 {
111 $foreground: 255;
112 }
113 @else {
114 $foreground: $background + $foreground;
115 }
116 @return $foreground;
117 }
118
119 //--------------------------------
120 // Linear Burn
121 //--------------------------------
122 @function blend-linearburn ($foreground, $background) {
123 $bm-red: blend-linearburn-color(red($foreground), red($background));
124 $bm-green: blend-linearburn-color(green($foreground), green($background));
125 $bm-blue: blend-linearburn-color(blue($foreground), blue($background));
126
127 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
128 }
129 @function blend-linearburn-color($foreground, $background) {
130 @if $background + $foreground < 255 {
131 $foreground: 0;
132 }
133 @else {
134 $foreground: $background + $foreground - 255;
135 }
136 @return $foreground;
137 }
138
139 //--------------------------------
140 // Difference
141 //--------------------------------
142 @function blend-difference ($foreground, $background) {
143 $bm-red: abs(red($background) - red($foreground));
144 $bm-green: abs(green($background) - green($foreground));
145 $bm-blue: abs(blue($background) - blue($foreground));
146
147 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
148 }
149
150 //--------------------------------
151 // Screen
152 //--------------------------------
153 @function blend-screen ($foreground, $background) {
154 $bm-red: blend-screen-color(red($foreground), red($background));
155 $bm-green: blend-screen-color(green($foreground), green($background));
156 $bm-blue: blend-screen-color(blue($foreground), blue($background));
157
158 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
159 }
160 @function blend-screen-color($foreground, $background) {
161 @return (255 - ( ( (255 - $foreground) * (255 - $background)) / 256));
162 }
163
164 //--------------------------------
165 // Exclusion
166 //--------------------------------
167 @function blend-exclusion ($foreground, $background) {
168 $bm-red: blend-exclusion-color(red($foreground), red($background));
169 $bm-green: blend-exclusion-color(green($foreground), green($background));
170 $bm-blue: blend-exclusion-color(blue($foreground), blue($background));
171
172 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
173 }
174 @function blend-exclusion-color($foreground, $background) {
175 @return $background - ($background * (2 / 255) - 1) * $foreground;
176 }
177
178 //--------------------------------
179 // Overlay
180 //--------------------------------
181 @function blend-overlay ($foreground, $background) {
182 $bm-red: blend-overlay-color(red($foreground), red($background));
183 $bm-green: blend-overlay-color(green($foreground), green($background));
184 $bm-blue: blend-overlay-color(blue($foreground), blue($background));
185
186 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
187 }
188 @function blend-overlay-color($foreground, $background) {
189 @if $background <= 255 / 2 {
190 $foreground: (2 * $background * $foreground) / 255;
191 } @else {
192 $foreground: 255 - (255 - 2 * ($background - (255 / 2))) * (255 - $foreground) / 255;
193 }
194 @return $foreground;
195 }
196
197 //--------------------------------
198 // Soft Light
199 //--------------------------------
200 @function blend-softlight ($foreground, $background) {
201 $bm-red: blend-softlight-color(red($foreground), red($background));
202 $bm-green: blend-softlight-color(green($foreground), green($background));
203 $bm-blue: blend-softlight-color(blue($foreground), blue($background));
204
205 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
206 }
207 @function blend-softlight-color($foreground, $background) {
208 @if $background < 128 {
209 $foreground: (($foreground / 2) + 64) * $background * (2 / 255);
210 } @else {
211 $foreground: 255 - (191 - ($foreground / 2)) * (255 - $background) * (2 / 255);
212 }
213 @return $foreground;
214 }
215
216 //--------------------------------
217 // Hard Light
218 //--------------------------------
219 @function blend-hardlight ($foreground, $background) {
220 $bm-red: blend-hardlight-color(red($foreground), red($background));
221 $bm-green: blend-hardlight-color(green($foreground), green($background));
222 $bm-blue: blend-hardlight-color(blue($foreground), blue($background));
223
224 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
225 }
226 @function blend-hardlight-color($foreground, $background) {
227 $tmp-blend: $foreground;
228 @if $tmp-blend < 128 {
229 $foreground: $background * $tmp-blend * (2 / 255);
230 } @else {
231 $foreground: 255 - (255-$background) * (255-$tmp-blend) * (2 / 255);
232 }
233 @return $foreground;
234 }
235
236 //--------------------------------
237 // Color Dodge
238 //--------------------------------
239 @function blend-colordodge ($foreground, $background) {
240 $bm-red: blend-colordodge-color(red($foreground), red($background));
241 $bm-green: blend-colordodge-color(green($foreground), green($background));
242 $bm-blue: blend-colordodge-color(blue($foreground), blue($background));
243
244 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
245 }
246 @function blend-colordodge-color($foreground, $background) {
247 $tmp: $background * 256 / (255 - $foreground);
248 @if $tmp > 255 or $foreground == 255 {
249 $foreground: 255;
250 } @else {
251 $foreground: $tmp;
252 }
253 @return $foreground;
254 }
255
256 //--------------------------------
257 // Color Burn
258 //--------------------------------
259 @function blend-colorburn ($foreground, $background) {
260 $bm-red: blend-colorburn-color(red($foreground), red($background));
261 $bm-green: blend-colorburn-color(green($foreground), green($background));
262 $bm-blue: blend-colorburn-color(blue($foreground), blue($background));
263
264 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
265 }
266 @function blend-colorburn-color($foreground, $background) {
267 $tmp: (255 - ((255 - $background) * 255) / $foreground);
268
269 // TODO: hacked to replicate photoshop
270 @if $foreground == 0 {
271 $foreground: 255;
272 } @elseif $tmp < 0 {
273 $foreground: 0;
274 } @else {
275 $foreground: $tmp;
276 }
277 @return $foreground;
278 }
279
280 //--------------------------------
281 // Linear Light
282 //--------------------------------
283 @function blend-linearlight ($foreground, $background) {
284 $bm-red: blend-linearlight-color(red($foreground), red($background));
285 $bm-green: blend-linearlight-color(green($foreground), green($background));
286 $bm-blue: blend-linearlight-color(blue($foreground), blue($background));
287
288 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
289 }
290 @function blend-linearlight-color($foreground, $background) {
291 @if $foreground < 128 {
292 $foreground: blend-linearburn-color($background, 2 * $foreground);
293 } @else {
294 $foreground: blend-lineardodge-color($background, 2 * ($foreground - 128));
295 }
296 @return $foreground;
297 }
298
299 //--------------------------------
300 // Vivid Light
301 //--------------------------------
302 @function blend-vividlight ($foreground, $background) {
303 $bm-red: blend-vividlight-color(red($foreground), red($background));
304 $bm-green: blend-vividlight-color(green($foreground), green($background));
305 $bm-blue: blend-vividlight-color(blue($foreground), blue($background));
306
307 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
308 }
309
310 @function blend-vividlight-color($foreground, $background) {
311 @if $foreground < 128 {
312 $foreground: blend-colorburn-color(2 * $foreground, $background);
313 } @else {
314 $foreground: blend-colordodge-color(2 * ($foreground - 128), $background);
315 }
316 @return $foreground;
317 }
318
319 //--------------------------------
320 // Pin Light
321 //--------------------------------
322 @function blend-pinlight ($foreground, $background) {
323 $bm-red: blend-pinlight-color(red($foreground), red($background));
324 $bm-green: blend-pinlight-color(green($foreground), green($background));
325 $bm-blue: blend-pinlight-color(blue($foreground), blue($background));
326
327 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
328 }
329
330 @function blend-pinlight-color($foreground, $background) {
331 @if $foreground < 128 {
332 $foreground: blend-darken-color($background, 2 * $foreground);
333 } @else {
334 $foreground: blend-lighten-color($background, 2 * ($foreground - 128));
335 }
336 @return $foreground;
337 }
338
339 //--------------------------------
340 // Hard Mix
341 //--------------------------------
342 @function blend-hardmix ($foreground, $background) {
343 $bm-red: blend-hardmix-color(red($foreground), red($background));
344 $bm-green: blend-hardmix-color(green($foreground), green($background));
345 $bm-blue: blend-hardmix-color(blue($foreground), blue($background));
346
347 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
348 }
349
350 @function blend-hardmix-color($foreground, $background) {
351 $tmp: blend-vividlight-color($foreground, $background);
352 @if $tmp < 128 {
353 $foreground: 0;
354 } @else {
355 $foreground: 255;
356 }
357 @return $foreground;
358 }
359
360
361 // Unique to Photoshop
362
363 //--------------------------------
364 // Color Blend
365 //--------------------------------
366 @function blend-colorblend ($foreground, $background) {
367 $foreground-hsv: color-to-hsv($foreground);
368 $background-hsv: color-to-hsv($background);
369
370 $bm-hsv: nth($foreground-hsv, 1), nth($foreground-hsv, 2), nth($background-hsv, 3);
371 $bm-color: hsv-to-color($bm-hsv);
372
373 @return blend-normal(rgba(red($bm-color), green($bm-color), blue($bm-color), opacity($foreground)), $background);
374 }
375
376 //--------------------------------
377 // Dissolve
378 //--------------------------------
379 @function blend-dissolve ($foreground, $background) {
380 // The Dissolve blend mode acts on transparent and partially transparent pixels
381 // it treats transparency as a pixel pattern and applies a diffusion dither pattern.
382 // @see http://photoblogstop.com/photoshop/photoshop-blend-modes-explained
383 @return $foreground;
384 }
385
386 //--------------------------------
387 // Divide
388 //--------------------------------
389 @function blend-divide ($foreground, $background) {
390 $bm-red: blend-divide-colors(red($foreground), red($background));
391 $bm-green: blend-divide-colors(green($foreground), green($background));
392 $bm-blue:blend-divide-colors(blue($foreground), blue($background));
393
394 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
395 }
396 @function blend-divide-colors($foreground, $background) {
397 @return min((($background / 255) / ($foreground / 255)) * 255, 255);
398 }
399
400 //--------------------------------
401 // Hue
402 //--------------------------------
403 @function blend-hue ($foreground, $background) {
404 $foreground-hsv: color-to-hsv($foreground);
405 $background-hsv: color-to-hsv($background);
406
407 $bm-hsv: nth($foreground-hsv, 1), nth($background-hsv, 2), nth($background-hsv, 3);
408 $bm-color: hsv-to-color($bm-hsv);
409
410 @return blend-normal(rgba(red($bm-color), green($bm-color), blue($bm-color), opacity($foreground)), $background);
411 }
412
413 //--------------------------------
414 // Luminosity
415 //--------------------------------
416 @function blend-luminosity ($foreground, $background) {
417 $foreground-hsv: color-to-hsv($foreground);
418 $background-hsv: color-to-hsv($background);
419
420 $bm-hsv: nth($background-hsv, 1), nth($background-hsv, 2), nth($foreground-hsv, 3);
421 $bm-color: hsv-to-color($bm-hsv);
422
423 @return blend-normal(rgba(red($bm-color), green($bm-color), blue($bm-color), opacity($foreground)), $background);
424 }
425
426 //--------------------------------
427 // Saturation
428 //--------------------------------
429 @function blend-saturation ($foreground, $background) {
430 $foreground-hsv: color-to-hsv($foreground);
431 $background-hsv: color-to-hsv($background);
432
433 $bm-hsv: nth($background-hsv, 1), nth($foreground-hsv, 2), nth($background-hsv, 3);
434 $bm-color: hsv-to-color($bm-hsv);
435
436 @return blend-normal(rgba(red($bm-color), green($bm-color), blue($bm-color), opacity($foreground)), $background);
437 }
438
439 //--------------------------------
440 // Subtract
441 //--------------------------------
442 @function blend-subtract ($foreground, $background) {
443 $bm-red: max(red($background) - red($foreground), 0);
444 $bm-green: max(green($background) - green($foreground), 0);
445 $bm-blue: max(blue($background) - blue($foreground), 0);
446
447 @return blend-normal(rgba($bm-red, $bm-green, $bm-blue, opacity($foreground)), $background);
448 }
449
450 //--------------------------------
451 // HSL and HSV
452 //--------------------------------
453 // @see https://gist.github.com/1069204
454 @function hsv-to-hsl($h, $s: 0, $v: 0) {
455 @if type-of($h) == 'list' {
456 $v: nth($h, 3);
457 $s: nth($h, 2);
458 $h: nth($h, 1);
459 }
460
461 @if unit($h) == 'deg' {
462 $h: 3.1415 * 2 * ($h / 360deg);
463 }
464 @if unit($s) == '%' {
465 $s: 0 + ($s / 100%);
466 }
467 @if unit($v) == '%' {
468 $v: 0 + ($v / 100%);
469 }
470
471 $ss: $s * $v;
472 $ll: (2 - $s) * $v;
473
474 @if $ll <= 1 and $ll != 0 {
475 $ss: $ss / $ll;
476 } @else if ($ll == 2) {
477 $ss: 0;
478 } @else {
479 $ss: $ss / (2 - $ll);
480 }
481
482 $ll: $ll / 2;
483
484 @return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $ss))), percentage(max(0, min(1, $ll)));
485 }
486
487 @function hsl-to-hsv($h, $ss: 0, $ll: 0) {
488 @if type-of($h) == 'list' {
489 $ll: nth($h, 3);
490 $ss: nth($h, 2);
491 $h: nth($h, 1);
492 } @else if type-of($h) == 'color' {
493 $ll: lightness($h);
494 $ss: saturation($h);
495 $h: hue($h);
496 }
497
498 @if unit($h) == 'deg' {
499 $h: 3.1415 * 2 * ($h / 360deg);
500 }
501 @if unit($ss) == '%' {
502 $ss: 0 + ($ss / 100%);
503 }
504 @if unit($ll) == '%' {
505 $ll: 0 + ($ll / 100%);
506 }
507
508 $ll: $ll * 2;
509
510 @if $ll <= 1 {
511 $ss: $ss * $ll;
512 } @else {
513 $ss: $ss * (2 - $ll);
514 }
515
516 $v: ($ll + $ss) / 2;
517 $s: if($ll + $ss == 0, 0, (2 * $ss) / ($ll + $ss));
518
519 @return 360deg * $h / (3.1415 * 2), percentage(max(0, min(1, $s))), percentage(max(0, min(1, $v)));
520 }
521
522 @function color-to-hsv($color) {
523 @return hsl-to-hsv($color);
524 }
525
526 @function hsv-to-color($h, $s: 0, $v: 0) {
527 $hsl: hsv-to-hsl($h, $s, $v);
528 @return hsl(nth($hsl, 1), nth($hsl, 2), nth($hsl, 3));
529 }