Skip to content

Is there any way to transform Image to Black and White mode, but not grey mode #1715

Answered by tylerrrkd
tylerrrkd asked this question in Q&A
Discussion options

You must be logged in to vote

@bennyli519 Hi friend, you could use my code.

import { Skia, Image, RuntimeShader } from '@shopify/react-native-skia'
import type { ImageProps, SkiaProps } from '@shopify/react-native-skia'

const BLACK_AND_WHITE_SHADER = Skia.RuntimeEffect.Make(`
uniform shader image;

vec4 main(vec2 xy)
{
    // Normalized pixel coordinates (from 0 to 1)
    vec4 texColor= image.eval(xy).rgba;
    
    // get gray scale or luminescence
    float gray = 0.33 * (texColor.r + texColor.g + texColor.b);
    
    // mix white and black by the value of step(threshold, gray)
    // you could adjust the threshold value
    float threshold = 0.5;
    if(gray > threshold) {
      // white
      texColor.rgb = vec3…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@bennyli519
Comment options

@tylerrrkd
Comment options

Answer selected by tylerrrkd
@tylerrrkd
Comment options

@bennyli519
Comment options

@AleVeron
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants