* Applies a minimum color threshold to a grayscale image. * Converts image to grayscale by default. * @example * ```ts * import { Jimp } from "jimp"; * * const image = await Jimp.read("test/image.png"); * * image.threshold({ max: 150 }); * ```
(image: I, options: ThresholdOptions)
| 28 | * ``` |
| 29 | */ |
| 30 | threshold<I extends JimpClass>(image: I, options: ThresholdOptions) { |
| 31 | let { |
| 32 | max, |
| 33 | replace = 255, |
| 34 | // eslint-disable-next-line prefer-const |
| 35 | autoGreyscale = true, |
| 36 | } = ThresholdOptionsSchema.parse(options); |
| 37 | |
| 38 | max = limit255(max); |
| 39 | replace = limit255(replace); |
| 40 | |
| 41 | if (autoGreyscale) { |
| 42 | color.greyscale(image); |
| 43 | } |
| 44 | |
| 45 | image.scan((_, __, idx) => { |
| 46 | const grey = |
| 47 | image.bitmap.data[idx]! < max ? image.bitmap.data[idx]! : replace; |
| 48 | |
| 49 | image.bitmap.data[idx] = grey; |
| 50 | image.bitmap.data[idx + 1] = grey; |
| 51 | image.bitmap.data[idx + 2] = grey; |
| 52 | }); |
| 53 | |
| 54 | return image; |
| 55 | }, |
| 56 | }; |