This repository was archived by the owner on Jul 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add setBlendedPixel for draw* functions #478
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b91cb35
feat: add setBlendedVisiblePixel to drawLineOnImage
EscapedGibbon 697c4a8
test: add testing case for drawLine
EscapedGibbon 8a82333
feat: add setBlendedVisiblePixlel to drawPoints
EscapedGibbon 21a6296
test: add tests to setBlendedVisiblePixel
EscapedGibbon dd9b988
feat: add setBlendedPixel on drawPolygon
EscapedGibbon 4af0d6b
feat: add setBlendedVisiblePixel to drawCircle
EscapedGibbon 688e98a
feat: add setBlendedVisiblePixel to drawRectangle
EscapedGibbon ede94d3
refactor: update passing of color argument for blended pixels
EscapedGibbon 7fe372f
test: update tests
EscapedGibbon c407b55
test: remove unnecessary test
EscapedGibbon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,63 @@ | ||
import { setBlendedVisiblePixel } from '../setBlendedVisiblePixel'; | ||
|
||
test('GREYA image, default options', () => { | ||
const image = testUtils.createGreyaImage([ | ||
[50, 255], | ||
[20, 30], | ||
]); | ||
setBlendedVisiblePixel(image, 0, 1); | ||
expect(image).toMatchImageData([ | ||
[50, 255], | ||
[0, 255], | ||
]); | ||
}); | ||
|
||
test('GREYA image: set pixel out of bounds', () => { | ||
const image = testUtils.createGreyaImage([ | ||
[50, 255, 1, 2, 3, 4], | ||
[20, 30, 5, 6, 7, 8], | ||
[1, 2, 3, 4, 5, 6], | ||
]); | ||
setBlendedVisiblePixel(image, 0, 5, { color: [40, 40] }); | ||
expect(image).toMatchImageData([ | ||
[50, 255, 1, 2, 3, 4], | ||
[20, 30, 5, 6, 7, 8], | ||
[1, 2, 3, 4, 5, 6], | ||
]); | ||
}); | ||
|
||
test('RGBA image: set pixel out of bounds', () => { | ||
const image = testUtils.createGreyaImage([ | ||
[50, 255, 1, 200, 2, 3, 4, 200], | ||
[20, 30, 5, 200, 6, 7, 8, 200], | ||
[1, 2, 3, 200, 4, 5, 6, 200], | ||
]); | ||
setBlendedVisiblePixel(image, 0, 5, { color: [40, 40, 40, 40] }); | ||
expect(image).toMatchImageData([ | ||
[50, 255, 1, 200, 2, 3, 4, 200], | ||
[20, 30, 5, 200, 6, 7, 8, 200], | ||
[1, 2, 3, 200, 4, 5, 6, 200], | ||
EscapedGibbon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]); | ||
}); | ||
|
||
test('GREYA image: alpha different from 255', () => { | ||
const image = testUtils.createGreyaImage([[50, 64]]); | ||
setBlendedVisiblePixel(image, 0, 0, { color: [100, 128] }); | ||
const alpha = 128 + 64 * (1 - 128 / 255); | ||
const component = (100 * 128 + 50 * 64 * (1 - 128 / 255)) / alpha; | ||
EscapedGibbon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(image).toMatchImageData([[component, alpha]]); | ||
}); | ||
|
||
test('asymetrical test', () => { | ||
const image = testUtils.createGreyaImage([ | ||
[50, 255, 1, 2, 3, 4], | ||
[20, 30, 5, 6, 7, 8], | ||
[1, 2, 3, 4, 5, 6], | ||
]); | ||
setBlendedVisiblePixel(image, 2, 0, { color: [0, 125] }); | ||
expect(image).toMatchImageData([ | ||
[50, 255, 1, 2, 0, 127], | ||
[20, 30, 5, 6, 7, 8], | ||
[1, 2, 3, 4, 5, 6], | ||
]); | ||
}); |
EscapedGibbon marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.