summaryrefslogtreecommitdiff
path: root/crates/typst-pdf/src/postscript
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-11-08 14:32:42 +0100
committerLaurenz <laurmaedje@gmail.com>2023-11-08 15:09:55 +0100
commit46846a337e8084acd46c70bccc2fca2659e9fb9a (patch)
tree9e9d6abf76867d823644e85e34571c70e07eea4f /crates/typst-pdf/src/postscript
parent80b4ca4c04cb5d911947895d9d04c87efb97b0f4 (diff)
Extract `typst-pdf` crate
Diffstat (limited to 'crates/typst-pdf/src/postscript')
-rw-r--r--crates/typst-pdf/src/postscript/hsl.ps63
-rw-r--r--crates/typst-pdf/src/postscript/hsv.ps62
-rw-r--r--crates/typst-pdf/src/postscript/oklab.ps78
3 files changed, 203 insertions, 0 deletions
diff --git a/crates/typst-pdf/src/postscript/hsl.ps b/crates/typst-pdf/src/postscript/hsl.ps
new file mode 100644
index 00000000..740bc3ed
--- /dev/null
+++ b/crates/typst-pdf/src/postscript/hsl.ps
@@ -0,0 +1,63 @@
+
+{
+ % Starting stack: H, S, L
+ % /!\ 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 = (1 - |2 * L - 1|) * S
+ dup 1 exch 2 mul 1 sub abs sub 3 2 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 = L - C / 2
+ exch dup 2 div 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
+} \ No newline at end of file
diff --git a/crates/typst-pdf/src/postscript/hsv.ps b/crates/typst-pdf/src/postscript/hsv.ps
new file mode 100644
index 00000000..b29adf11
--- /dev/null
+++ b/crates/typst-pdf/src/postscript/hsv.ps
@@ -0,0 +1,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
+} \ No newline at end of file
diff --git a/crates/typst-pdf/src/postscript/oklab.ps b/crates/typst-pdf/src/postscript/oklab.ps
new file mode 100644
index 00000000..4d6e9ad5
--- /dev/null
+++ b/crates/typst-pdf/src/postscript/oklab.ps
@@ -0,0 +1,78 @@
+{
+ % Starting stack: L, A, B
+ % /!\ WARNING: The A and B components **MUST** be encoded
+ % in the range [0, 1] before calling this function.
+ % This is because the function assumes that the
+ % A and B components are offset by a factor of 0.4
+ % in order to meet the range requirements of the
+ % PDF specification.
+
+ exch 0.4 sub
+ exch 0.4 sub
+
+ % Load L a and b into the stack
+ 2 index
+ 2 index
+ 2 index
+
+ % Compute f1 = ((0.3963377774 * a) + (0.2158037573 * b) + L)^3
+ 0.2158037573 mul exch
+ 0.3963377774 mul add add
+ dup dup mul mul
+
+ % Load L, a, and b into the stack
+ 3 index
+ 3 index
+ 3 index
+
+ % Compute f2 = ((-0.1055613458 * a) + (-0.0638541728 * b) + L)^3
+ -0.0638541728 mul exch
+ -0.1055613458 mul add add
+ dup dup mul mul
+
+ % Load L, a, and b into the stack
+ 4 index
+ 4 index
+ 4 index
+
+ % Compute f3 = ((-0.0894841775 * a) + (-1.2914855480 * b) + L)^3
+ -1.2914855480 mul exch
+ -0.0894841775 mul add add
+ dup dup mul mul
+
+ % Discard L, a, and b by rolling the stack and popping
+ 6 3 roll pop pop pop
+
+ % Load f1, f2, and f3 into the stack
+ 2 index
+ 2 index
+ 2 index
+
+ % Compute R = f1 * 4.0767416621 + f2 * -3.3077115913 + f3 * 0.2309699292
+ 0.2309699292 mul exch
+ -3.3077115913 mul add exch
+ 4.0767416621 mul add
+
+ % Load f1, f2, and f3 into the stack
+ 3 index
+ 3 index
+ 3 index
+
+ % Compute G = f1 * -1.2684380046 + f2 * 2.6097574011 + f3 * -0.3413193965
+ -0.3413193965 mul exch
+ 2.6097574011 mul add exch
+ -1.2684380046 mul add
+
+ % Load f1, f2, and f3 into the stack
+ 4 index
+ 4 index
+ 4 index
+
+ % Compute B = f1 * -0.0041960863 + f2 * -0.7034186147 + f3 * 1.7076147010
+ 1.7076147010 mul exch
+ -0.7034186147 mul add exch
+ -0.0041960863 mul add
+
+ % Discard f1, f2, and f3 by rolling the stack and popping
+ 6 3 roll pop pop pop
+} \ No newline at end of file