aboutsummaryrefslogtreecommitdiffstats
path: root/colour.c
blob: 8b5c4016d088097973c4b9c95cac86302a087cb4 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


double
srgb_encode_unsigned(double t)
{
	return t <= 0.0031306684425217108
		? 12.92 * t
		: fma(1.055, pow(t, 1 / 2.4), -0.055);
}

double
srgb_decode_unsigned(double t)
{
	return t <= 0.0031306684425217108 * 12.92
		? t / 12.92
		: pow((t + 0.055) / 1.055, 2.4);
}


double
srgb_blend(double at0, double t, double at1)
{
	at0 = srgb_decode_unsigned(at0);
	at1 = srgb_decode_unsigned(at1);
	return srgb_encode_unsigned(fma((1 - t), at0, t * at1));
}


void
set_source_colour_blend(cairo_t *g, const GdkColor *at0, double t, const GdkColor *at1)
{
	double max   = (double)UINT16_MAX;
	double red   = srgb_blend(at0->red   / max, t, at1->red   / max);
	double green = srgb_blend(at0->green / max, t, at1->green / max);
	double blue  = srgb_blend(at0->blue  / max, t, at1->blue  / max);
	cairo_set_source_rgb(g, red, green, blue);
}


void
set_source_colour(cairo_t *g, const GdkColor *colour)
{
	double max = (double)UINT16_MAX;
	cairo_set_source_rgb(g, colour->red / max, colour->green / max, colour->blue / max);
}