Method: Magick::Image#level_colors

Defined in:
ext/RMagick/rmimage.cpp

#level_colors(black_color = "black", white_color = "white", invert = true, channel = Magick::AllChannels) ⇒ Magick::Image #level_colors(black_color = "black", white_color = "white", invert = true, *channels) ⇒ Magick::Image

When invert is true, black and white will be mapped to the black_color and white_color colors, compressing all other colors linearly. When invert is false, black and white will be mapped to the black_color and white_color colors, stretching all other colors linearly.

Overloads:

  • #level_colors(black_color = "black", white_color = "white", invert = true, channel = Magick::AllChannels) ⇒ Magick::Image

    Parameters:

    • black_color (Magick::Pixel, String) (defaults to: "black")

      The color to be mapped to black

    • white_color (Magick::Pixel, String) (defaults to: "white")

      The color to be mapped to white

    • invert (defaults to: true)

      See the description above

    • channel (Magick::ChannelType) (defaults to: Magick::AllChannels)

      a ChannelType arguments.

  • #level_colors(black_color = "black", white_color = "white", invert = true, *channels) ⇒ Magick::Image

    Parameters:

    • black_color (Magick::Pixel, String) (defaults to: "black")

      The color to be mapped to black

    • white_color (Magick::Pixel, String) (defaults to: "white")

      The color to be mapped to white

    • invert (defaults to: true)

      See the description above

    • *channels (Magick::ChannelType)

      one or more ChannelType arguments.

Returns:



8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
# File 'ext/RMagick/rmimage.cpp', line 8630

VALUE
Image_level_colors(int argc, VALUE *argv, VALUE self)
{
    Image *image, *new_image;
    MagickPixel black_color, white_color;
    ChannelType channels;
    MagickBooleanType invert = MagickTrue;
    MagickBooleanType okay;
#if defined(IMAGEMAGICK_7)
    ExceptionInfo *exception;
#endif

    image = rm_check_destroyed(self);

    channels = extract_channels(&argc, argv);

    rm_init_magickpixel(image, &white_color);
    rm_init_magickpixel(image, &black_color);

    switch (argc)
    {
        case 3:
            invert = (MagickBooleanType)RTEST(argv[2]);

        case 2:
            Color_to_MagickPixel(image, &white_color, argv[1]);
            Color_to_MagickPixel(image, &black_color, argv[0]);
            break;

        case 1:
            rm_set_magickpixel(&white_color, "white");
            Color_to_MagickPixel(image, &black_color, argv[0]);
            break;

        case 0:
            rm_set_magickpixel(&white_color, "white");
            rm_set_magickpixel(&black_color, "black");
            break;

        default:
            raise_ChannelType_error(argv[argc-1]);
            break;
    }

    new_image = rm_clone_image(image);

#if defined(IMAGEMAGICK_7)
    exception = AcquireExceptionInfo();
    BEGIN_CHANNEL_MASK(new_image, channels);
    GVL_STRUCT_TYPE(LevelImageColors) args = { new_image, &black_color, &white_color, invert, exception };
    void *ret = CALL_FUNC_WITHOUT_GVL(GVL_FUNC(LevelImageColors), &args);
    okay = static_cast<MagickBooleanType>(reinterpret_cast<intptr_t &>(ret));
    END_CHANNEL_MASK(new_image);
    rm_check_exception(exception, new_image, DestroyOnError);
    DestroyExceptionInfo(exception);
#else
    GVL_STRUCT_TYPE(LevelColorsImageChannel) args = { new_image, channels, &black_color, &white_color, invert };
    void *ret = CALL_FUNC_WITHOUT_GVL(GVL_FUNC(LevelColorsImageChannel), &args);
    okay = static_cast<MagickBooleanType>(reinterpret_cast<intptr_t &>(ret));
    rm_check_image_exception(new_image, DestroyOnError);
#endif
    if (!okay)
    {
        rb_raise(rb_eRuntimeError, "LevelImageColors failed for unknown reason.");
    }

    return rm_image_new(new_image);
}