Skip to content

Commit

Permalink
feat(color): add Int32.alpha accessor, minor update int->srgb
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 4, 2021
1 parent 7ecfa0c commit b65f9ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/color/src/int/int-srgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const intArgb32Srgb = (out: Color | null, src: number) =>
((src >>> 16) & 0xff) * INV8BIT,
((src >>> 8) & 0xff) * INV8BIT,
(src & 0xff) * INV8BIT,
((src >>> 24) & 0xff) * INV8BIT
(src >>> 24) * INV8BIT
);

export const intAbgr32Srgb = (out: Color | null, src: number) =>
Expand All @@ -17,7 +17,7 @@ export const intAbgr32Srgb = (out: Color | null, src: number) =>
(src & 0xff) * INV8BIT,
((src >>> 8) & 0xff) * INV8BIT,
((src >>> 16) & 0xff) * INV8BIT,
((src >>> 24) & 0xff) * INV8BIT
(src >>> 24) * INV8BIT
);

export const intRgb24Srgb = (out: Color | null, src: number) =>
Expand Down
19 changes: 17 additions & 2 deletions packages/color/src/int/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isString,
} from "@thi.ng/checks";
import { illegalArgs } from "@thi.ng/errors";
import { clamp01 } from "@thi.ng/math";
import { IRandom, SYSTEM } from "@thi.ng/random";
import { declareIndex, eqDelta4, mapStridedBuffer } from "@thi.ng/vectors";
import type {
Expand Down Expand Up @@ -37,6 +38,15 @@ export abstract class Int32 {
get range(): [ReadonlyColor, ReadonlyColor] {
return [[0], [0xffffffff]];
}

get alpha() {
return (this[0] >>> 24) / 255;
}

set alpha(a: number) {
this[0] = (this[0] & 0xffffff) | ((clamp01(a) * 0xff + 0.5) << 24);
}

*[Symbol.iterator]() {
yield this[0];
}
Expand Down Expand Up @@ -155,11 +165,16 @@ const defInt = <T extends Int32>(
factory.range = <[ReadonlyColor, ReadonlyColor]>[[0], [0xffffffff]];

factory.random = (
rnd?: IRandom,
rnd: IRandom = SYSTEM,
buf?: NumericArray,
idx?: number,
stride?: number
) => <any>new ctor(buf, idx, stride).randomize(rnd);
) =>
<any>(
new ctor(buf, idx, stride).set([
(rnd.int() & 0xffffff) | 0xff000000,
])
);

factory.mapBuffer = (
buf: NumericArray,
Expand Down

0 comments on commit b65f9ee

Please sign in to comment.