summaryrefslogtreecommitdiff
path: root/crates/typst-pdf/src/postscript/hsv.ps
blob: b29adf11ddc7d4e5499b143fdc9b2d42cd41df00 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{
    % Starting stack: H, S, V
    % /!\ WARNING: The hue component **MUST** be encoded
    %     in the range [0, 1] before calling this function.
    %     This is because the function assumes that the
    %     hue component are divided by a factor of 360
    %     in order to meet the range requirements of the
    %     PDF specification.

    % First we do H = (H * 360.0) % 360
    3 2 roll 360 mul 3 1 roll

    % Compute C = V * S
    dup 3 1 roll mul

    % P = (H / 60) % 2
    3 2 roll dup 60 div 2
    2 copy div cvi mul exch sub abs

    % X = C * (1 - |P - 1|)
    1 exch 1 sub abs sub 3 2 roll dup 3 1 roll mul

    % Compute m = V - C
    exch dup 5 4 roll exch sub

    % Rotate so H is top
    4 3 roll exch 4 1 roll

    % Construct the RGB stack
    dup 60 lt {
        % We need to build: (C, X, 0)
        pop 0 3 1 roll
    } {
        dup 120 lt {
            % We need to build: (X, C, 0)
            pop exch 0 3 1 roll
        } {
            dup 180 lt {
                % We need to build: (0, C, X)
                pop 0
            } {
                dup 240 lt {
                    % We need to build: (0, X, C)
                    pop exch 0
                } {
                    300 lt {
                        % We need to build: (X, 0, C)
                        0 3 2 roll
                    } {
                        % We need to build: (C, 0, X)
                        0 exch
                    } ifelse
                } ifelse
            } ifelse
        } ifelse
    } ifelse

    4 3 roll

    % Add m to each component
    dup dup 6 2 roll add 5 2 roll add exch 4 3 roll add exch
}