-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add setBlendedVisiblePixel to drawLineOnImage
- Loading branch information
1 parent
ec04a57
commit b91cb35
Showing
2 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
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
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,67 @@ | ||
import { Image } from '../Image'; | ||
import { Mask } from '../Mask'; | ||
|
||
import { getDefaultColor } from './getDefaultColor'; | ||
import { assert } from './validators/assert'; | ||
|
||
export interface SetBlendedVisiblePixelOptions { | ||
/** | ||
* Color with which to blend the image pixel. | ||
* @default `'Opaque black'`. | ||
*/ | ||
color?: number[]; | ||
} | ||
|
||
/** | ||
* Blend the given pixel with the pixel at the specified location in the image if the pixel is in image's bounds. | ||
* @param image - The image with which to blend. | ||
* @param column - Column of the target pixel. | ||
* @param row - Row of the target pixel. | ||
* @param options - Set blended pixel options. | ||
*/ | ||
export function setBlendedVisiblePixel( | ||
image: Image | Mask, | ||
column: number, | ||
row: number, | ||
options: SetBlendedVisiblePixelOptions = {}, | ||
) { | ||
const { color = getDefaultColor(image) } = options; | ||
|
||
if (!image.alpha) { | ||
image.setVisiblePixel(column, row, color); | ||
} else { | ||
assert(image instanceof Image); | ||
|
||
const sourceAlpha = color.at(-1) as number; | ||
|
||
if (sourceAlpha === image.maxValue) { | ||
image.setVisiblePixel(column, row, color); | ||
return; | ||
} | ||
|
||
const targetAlpha = image.getValue(column, row, image.channels - 1); | ||
|
||
const newAlpha = | ||
sourceAlpha + targetAlpha * (1 - sourceAlpha / image.maxValue); | ||
if (column >= 0 && column < image.width && row >= 0 && row < image.height) { | ||
image.setValue(column, row, image.channels - 1, newAlpha); | ||
} | ||
for (let component = 0; component < image.components; component++) { | ||
const sourceComponent = color[component]; | ||
const targetComponent = image.getValue(column, row, component); | ||
|
||
const newComponent = | ||
(sourceComponent * sourceAlpha + | ||
targetComponent * targetAlpha * (1 - sourceAlpha / image.maxValue)) / | ||
newAlpha; | ||
if ( | ||
column >= 0 && | ||
column < image.width && | ||
row >= 0 && | ||
row < image.height | ||
) { | ||
image.setValue(column, row, component, newComponent); | ||
} | ||
} | ||
} | ||
} |