-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/AcrobatDC_js_api_reference.pdf * Add color, fullscreen objects + few constants.
- Loading branch information
1 parent
a06f487
commit 7a7ac3b
Showing
8 changed files
with
1,049 additions
and
8 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* Copyright 2020 Mozilla Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { PDFObject } from "./pdf_object.js"; | ||
|
||
class Color extends PDFObject { | ||
constructor() { | ||
super({}); | ||
|
||
this.transparent = ["T"]; | ||
this.black = ["G", 0]; | ||
this.white = ["G", 1]; | ||
this.red = ["RGB", 1, 0, 0]; | ||
this.green = ["RGB", 0, 1, 0]; | ||
this.blue = ["RGB", 0, 0, 1]; | ||
this.cyan = ["CMYK", 1, 0, 0, 0]; | ||
this.magenta = ["CMYK", 0, 1, 0, 0]; | ||
this.yellow = ["CMYK", 0, 0, 1, 0]; | ||
this.dkGray = ["G", 0.25]; | ||
this.gray = ["G", 0.5]; | ||
this.ltGray = ["G", 0.75]; | ||
|
||
this._converters = { | ||
CMYK_G(components) { | ||
const [c, y, m, k] = components; | ||
return ["G", 1 - Math.min(1, 0.3 * c + 0.59 * m + 0.11 * y + k)]; | ||
}, | ||
G_CMYK(components) { | ||
const [g] = components; | ||
return ["CMYK", 0, 0, 0, 1 - g]; | ||
}, | ||
G_RGB(components) { | ||
const [g] = components; | ||
return ["RGB", g, g, g]; | ||
}, | ||
RGB_G(components) { | ||
const [r, g, b] = components; | ||
return ["G", 0.3 * r + 0.59 * g + 0.11 * b]; | ||
}, | ||
CMYK_RGB(components) { | ||
const [c, y, m, k] = components; | ||
return [ | ||
"RGB", | ||
1 - Math.min(1, c + k), | ||
1 - Math.min(1, m + k), | ||
1 - Math.min(1, y + k), | ||
]; | ||
}, | ||
RGB_CMYK(components) { | ||
const [r, g, b] = components; | ||
const c = 1 - r; | ||
const m = 1 - g; | ||
const y = 1 - b; | ||
return ["CMYK", c, m, y, Math.min(c, Math.min(m, y))]; | ||
}, | ||
}; | ||
} | ||
|
||
_isValidSpace(cColorSpace) { | ||
return ( | ||
typeof cColorSpace === "string" && | ||
(cColorSpace === "T" || | ||
cColorSpace === "G" || | ||
cColorSpace === "RGB" || | ||
cColorSpace === "CMYK") | ||
); | ||
} | ||
|
||
_getCorrectColor(colorArray) { | ||
if (!Array.isArray(colorArray) || colorArray.length === 0) { | ||
return this.black; | ||
} | ||
const space = colorArray[0]; | ||
if (!this._isValidSpace(space)) { | ||
return this.black; | ||
} | ||
|
||
if (space === "T" && colorArray.length !== 1) { | ||
return this.black; | ||
} | ||
|
||
if (space === "G" && colorArray.length !== 2) { | ||
return this.black; | ||
} | ||
|
||
if (space === "RGB" && colorArray.length !== 4) { | ||
return this.black; | ||
} | ||
|
||
if (space === "CMYK" && colorArray.length !== 5) { | ||
return this.black; | ||
} | ||
|
||
if ( | ||
colorArray.slice(1).some(c => typeof c !== "number" || c < 0 || c > 1) | ||
) { | ||
return this.black; | ||
} | ||
|
||
return colorArray; | ||
} | ||
|
||
convert(colorArray, cColorSpace) { | ||
if (!this._isValidSpace(cColorSpace)) { | ||
return this.black; | ||
} | ||
|
||
if (cColorSpace === "T") { | ||
return ["T"]; | ||
} | ||
|
||
colorArray = this._getCorrectColor(colorArray); | ||
if (colorArray[0] === cColorSpace) { | ||
return colorArray; | ||
} | ||
|
||
if (colorArray[0] === "T") { | ||
return this.convert(this.black, cColorSpace); | ||
} | ||
|
||
return this._converters[`${colorArray[0]}_${cColorSpace}`]( | ||
colorArray.slice(1) | ||
); | ||
} | ||
|
||
equal(colorArray1, colorArray2) { | ||
colorArray1 = this._getCorrectColor(colorArray1); | ||
colorArray2 = this._getCorrectColor(colorArray2); | ||
|
||
if (colorArray1[0] === "T" || colorArray2[0] === "T") { | ||
return colorArray1[0] === "T" && colorArray2[0] === "T"; | ||
} | ||
|
||
if (colorArray1[0] !== colorArray2[0]) { | ||
colorArray2 = this.convert(colorArray2, colorArray1[0]); | ||
} | ||
|
||
return colorArray1.slice(1).every((c, i) => c === colorArray2[i + 1]); | ||
} | ||
} | ||
|
||
export { Color }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.