Skip to content

Commit

Permalink
Prettify!
Browse files Browse the repository at this point in the history
  • Loading branch information
atesgoral committed Aug 7, 2021
1 parent 4e2a746 commit f9fa856
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
4 changes: 4 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"bracketSpacing": false
}
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.0.1] - 2021-07-16

*Deprecated. Can't be actually imported.*
_Deprecated. Can't be actually imported._

### Added

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const book = {
{name: 'Green', code: 'GREEN ', components: [0, 255, 0]},
{name: 'Blue', code: 'BLUE ', components: [0, 0, 255]},
],
isSpot: true
isSpot: true,
};
```

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"dist"
],
"scripts": {
"format": "prettier --write .",
"format-check": "prettier --check .",
"build": "rollup --config"
},
"repository": {
Expand All @@ -40,6 +42,7 @@
"@rollup/plugin-typescript": "^8.2.5",
"@tsconfig/node12": "^1.0.9",
"@types/node": "^16.4.13",
"prettier": "^2.3.2",
"rollup": "^2.56.0",
"tslib": "^2.3.0",
"typescript": "^4.3.5"
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineConfig({
external: ['stream-parser', 'stream'],
plugins: [typescript({tsconfig: './tsconfig.json'})],
output: [
{ file: pkg.main, format: 'cjs' },
{ file: pkg.module, format: 'es' }
]
{file: pkg.main, format: 'cjs'},
{file: pkg.module, format: 'es'},
],
});
47 changes: 28 additions & 19 deletions src/lib/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {ColorSpace, Color, ColorBook} from './types';
const IdToColorSpace: Record<number, ColorSpace> = {
0: 'RGB',
2: 'CMYK',
7: 'Lab'
7: 'Lab',
};

export interface AcbStreamDecoder extends Transform {
Expand All @@ -28,7 +28,7 @@ export class AcbStreamDecoder extends Transform implements AcbStreamDecoder {
pageMidPoint: 0,
colorSpace: 'RGB',
colors: [],
isSpot: false
isSpot: false,
};
private colorCount = 0;

Expand Down Expand Up @@ -94,10 +94,13 @@ export class AcbStreamDecoder extends Transform implements AcbStreamDecoder {
}

private onColorSpaceId(colorSpaceId: number) {
const colorSpace = IdToColorSpace[colorSpaceId]
const colorSpace = IdToColorSpace[colorSpaceId];

if (!colorSpace) {
return this.emit('error', new Error(`Unknown color space: ${colorSpaceId}`));
return this.emit(
'error',
new Error(`Unknown color space: ${colorSpaceId}`)
);
}

this.book.colorSpace = colorSpace;
Expand Down Expand Up @@ -125,7 +128,7 @@ export class AcbStreamDecoder extends Transform implements AcbStreamDecoder {
const color: Color = {
name: '',
code: '',
components: []
components: [],
};

this.readString((name) => {
Expand All @@ -145,19 +148,23 @@ export class AcbStreamDecoder extends Transform implements AcbStreamDecoder {

private readComponents(callback: (components: number[]) => void) {
switch (this.book.colorSpace) {
case 'RGB':
this._bytes(3, (chunk) => callback(Array.from(chunk)));
break;
case 'CMYK':
this._bytes(4, (chunk) => callback(Array.from(chunk).map((c) => Math.round((255 - c) / 2.55))));
break;
case 'Lab':
this._bytes(3, (chunk) => callback([
Math.round(chunk[0] / 2.55),
chunk[1] - 128,
chunk[2] - 128
]));
break;
case 'RGB':
this._bytes(3, (chunk) => callback(Array.from(chunk)));
break;
case 'CMYK':
this._bytes(4, (chunk) =>
callback(Array.from(chunk).map((c) => Math.round((255 - c) / 2.55)))
);
break;
case 'Lab':
this._bytes(3, (chunk) =>
callback([
Math.round(chunk[0] / 2.55),
chunk[1] - 128,
chunk[2] - 128,
])
);
break;
}
}

Expand All @@ -176,7 +183,9 @@ export class AcbStreamDecoder extends Transform implements AcbStreamDecoder {
private readString(callback: (value: string) => void) {
this.readUInt32BE((length) => {
if (length) {
this._bytes(length * 2, (chunk) => callback.call(this, chunk.swap16().toString('utf16le')));
this._bytes(length * 2, (chunk) =>
callback.call(this, chunk.swap16().toString('utf16le'))
);
} else {
callback.call(this, '');
}
Expand Down
34 changes: 18 additions & 16 deletions src/lib/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {ColorSpace, ColorBook} from './types';
import * as Chunk from './chunk';

const ColorSpaceToId: Record<ColorSpace, number> = {
'RGB': 0,
'CMYK': 2,
'Lab': 7
RGB: 0,
CMYK: 2,
Lab: 7,
};

export function* encodeAcb(book: ColorBook) {
Expand Down Expand Up @@ -33,19 +33,21 @@ export function* encodeAcb(book: ColorBook) {
yield Chunk.fromAscii(color.code);

switch (book.colorSpace) {
case 'RGB':
yield Buffer.from(color.components);
break;
case 'CMYK':
yield Buffer.from(color.components.map((c) => 255 - Math.round(c * 2.55)));
break;
case 'Lab':
yield Buffer.from([
Math.round(color.components[0] * 2.55),
color.components[1] + 128,
color.components[2] + 128
]);
break;
case 'RGB':
yield Buffer.from(color.components);
break;
case 'CMYK':
yield Buffer.from(
color.components.map((c) => 255 - Math.round(c * 2.55))
);
break;
case 'Lab':
yield Buffer.from([
Math.round(color.components[0] * 2.55),
color.components[1] + 128,
color.components[2] + 128,
]);
break;
}
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ picomatch@^2.2.2:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
integrity sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==

prettier@^2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.3.2.tgz#ef280a05ec253712e486233db5c6f23441e7342d"
integrity sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==

resolve@^1.17.0:
version "1.20.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975"
Expand Down

0 comments on commit f9fa856

Please sign in to comment.