Class: Magick::Pixel

Inherits:
Object
  • Object
show all
Includes:
Comparable, Observable
Defined in:
lib/rmagick_internal.rb,
ext/RMagick/rmmain.c

Overview

Magick::ImageList

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red = 0, green = 0, blue = 0, opacity = 0) ⇒ Magick::Pixel

Initialize Pixel object.

Returns self.

Parameters:

  • red (Numeric) (defaults to: 0)

    The red value

  • green (Numeric) (defaults to: 0)

    The green value

  • blue (Numeric) (defaults to: 0)

    The blue value

  • opacity (Numeric) (defaults to: 0)

    The opacity value



938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'ext/RMagick/rmpixel.c', line 938

VALUE
Pixel_initialize(int argc, VALUE *argv, VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

#if defined(IMAGEMAGICK_7)
    pixel->alpha = OpaqueAlpha;
#endif

    switch(argc)
    {
        case 4:
#if defined(IMAGEMAGICK_7)
            if (argv[3] != Qnil)
            {
                pixel->alpha = APP2QUANTUM(argv[3]);
            }
#else
            if (argv[3] != Qnil)
            {
                pixel->opacity = APP2QUANTUM(argv[3]);
            }
#endif
        case 3:
            if (argv[2] != Qnil)
            {
                pixel->blue = APP2QUANTUM(argv[2]);
            }
        case 2:
            if (argv[1] != Qnil)
            {
                pixel->green = APP2QUANTUM(argv[1]);
            }
        case 1:
            if (argv[0] != Qnil)
            {
                pixel->red = APP2QUANTUM(argv[0]);
            }
        case 0:
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 4)", argc);
    }

    return self;
}

Class Method Details

.from_color(name) ⇒ Magick::Pixel

Construct an Magick::Pixel corresponding to the given color name.

  • The “inverse” is Image#to_color, b/c the conversion of a pixel to a color name requires both a color depth and if the opacity value has meaning.

Parameters:

  • name (String)

    the color name

Returns:

See Also:



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'ext/RMagick/rmpixel.c', line 681

VALUE
Pixel_from_color(VALUE class ATTRIBUTE_UNUSED, VALUE name)
{
    PixelColor pp;
    ExceptionInfo *exception;
    MagickBooleanType okay;

    exception = AcquireExceptionInfo();
    okay = QueryColorCompliance(StringValueCStr(name), AllCompliance, &pp, exception);
    CHECK_EXCEPTION();
    DestroyExceptionInfo(exception);

    if (!okay)
    {
        rb_raise(rb_eArgError, "invalid color name: %s", StringValueCStr(name));
    }

    return Pixel_from_PixelColor(&pp);
}

.from_hsla(hue, saturation, lightness, alpha = 1.0) ⇒ Magick::Pixel

Construct an RGB pixel.

  • 0 <= hue < 360 OR “0%” <= hue < “100%”

  • 0 <= saturation <= 255 OR “0%” <= saturation <= “100%”

  • 0 <= lightness <= 255 OR “0%” <= lightness <= “100%”

  • 0 <= alpha <= 1 (0 is transparent, 1 is opaque) OR “0%” <= alpha <= “100%”

Returns a new Magick::Pixel object.

Parameters:

  • hue (Numeric, String)

    A value in the range.

  • saturation (Numeric, String)

    A value in the range.

  • lightness (Numeric, String)

    A value in the range.

  • alpha (Numeric) (defaults to: 1.0)

    The alpha value.

Returns:



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'ext/RMagick/rmpixel.c', line 717

VALUE
Pixel_from_hsla(int argc, VALUE *argv, VALUE class ATTRIBUTE_UNUSED)
{
    double h, s, l, a = 1.0;
    MagickPixel pp;
    ExceptionInfo *exception;
    char name[50];
    MagickBooleanType alpha = MagickFalse;

    switch (argc)
    {
        case 4:
            a = rm_percentage(argv[3], 1.0);
            alpha = MagickTrue;
        case 3:
            // saturation and lightness are out of 255 in new ImageMagicks and
            // out of 100 in old ImageMagicks. Compromise: always use %.
            l = rm_percentage(argv[2], 255.0);
            s = rm_percentage(argv[1], 255.0);
            h = rm_percentage(argv[0], 360.0);
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (%d for 3 or 4)", argc);
            break;
    }

    if (alpha && (a < 0.0 || a > 1.0))
    {
        rb_raise(rb_eRangeError, "alpha %g out of range [0.0, 1.0]", a);
    }
    if (l < 0.0 || l > 255.0)
    {
        rb_raise(rb_eRangeError, "lightness %g out of range [0.0, 255.0]", l);
    }
    if (s < 0.0 || s > 255.0)
    {
        rb_raise(rb_eRangeError, "saturation %g out of range [0.0, 255.0]", s);
    }
    if (h < 0.0 || h >= 360.0)
    {
        rb_raise(rb_eRangeError, "hue %g out of range [0.0, 360.0)", h);
    }

    memset(name, 0, sizeof(name));
    if (alpha)
    {
        snprintf(name, sizeof(name), "hsla(%-2.1f,%-2.1f,%-2.1f,%-2.1f)", h, s, l, a);
    }
    else
    {
        snprintf(name, sizeof(name), "hsl(%-2.1f,%-2.1f,%-2.1f)", h, s, l);
    }

    exception = AcquireExceptionInfo();

#if defined(IMAGEMAGICK_7)
    QueryColorCompliance(name, AllCompliance, &pp, exception);
#else
    QueryMagickColor(name, &pp, exception);
#endif
    CHECK_EXCEPTION();

    DestroyExceptionInfo(exception);

    return Pixel_from_MagickPixel(&pp);
}

Instance Method Details

#<=>(other) ⇒ -1, ...

Support Comparable mixin.

Parameters:

  • other (Object)

    the other Pixel

Returns:

  • (-1, 0, 1, nil)

    the result of compare



1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'ext/RMagick/rmpixel.c', line 1067

VALUE
Pixel_spaceship(VALUE self, VALUE other)
{
    Pixel *this, *that;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, this);
    TypedData_Get_Struct(other, Pixel, &rm_pixel_data_type, that);

    if (this->red != that->red)
    {
        return INT2NUM((this->red - that->red)/abs((int)(this->red - that->red)));
    }
    else if(this->green != that->green)
    {
        return INT2NUM((this->green - that->green)/abs((int)(this->green - that->green)));
    }
    else if(this->blue != that->blue)
    {
        return INT2NUM((this->blue - that->blue)/abs((int)(this->blue - that->blue)));
    }
#if defined(IMAGEMAGICK_7)
    else if(this->alpha != that->alpha)
    {
        return INT2NUM((this->alpha - that->alpha)/abs((int)(this->alpha - that->alpha)));
    }
#else
    else if(this->opacity != that->opacity)
    {
        return INT2NUM(((QuantumRange - this->opacity) - (QuantumRange - that->opacity))/abs((int)((QuantumRange - this->opacity) - (QuantumRange - that->opacity))));
    }
#endif

    // Values are equal, check class.

    return rb_funcall(CLASS_OF(self), rb_intern("<=>"), 1, CLASS_OF(other));

}

#===(other) ⇒ Boolean

“Case equal” operator for Pixel.

Parameters:

  • other (Object)

    the other object

Returns:

  • (Boolean)

    true or false



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'ext/RMagick/rmpixel.c', line 512

VALUE
Pixel_case_eq(VALUE self, VALUE other)
{
    if (CLASS_OF(self) == CLASS_OF(other))
    {
        Pixel *this, *that;

        TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, this);
        TypedData_Get_Struct(other, Pixel, &rm_pixel_data_type, that);
        return (this->red == that->red
            && this->blue == that->blue
            && this->green == that->green
#if defined(IMAGEMAGICK_7)
            && this->alpha == that->alpha) ? Qtrue : Qfalse;
#else
            && this->opacity == that->opacity) ? Qtrue : Qfalse;
#endif
    }

    return Qfalse;
}

#alphaNumeric

Get Pixel alpha value.

Returns:

  • (Numeric)

    the alpha value



100
101
102
103
104
105
106
107
108
109
110
# File 'ext/RMagick/rmpixel.c', line 100

VALUE
Pixel_alpha(VALUE self)
{
    Pixel *pixel;
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
#if defined(IMAGEMAGICK_7)
    return C_int_to_R_int(pixel->alpha);
#else
    return C_int_to_R_int(QuantumRange - pixel->opacity);
#endif
}

#alpha=(v) ⇒ Numeric

Set Pixel alpha value.

  • Pixel is Observable. Setters call #changed, #notify_observers

  • Setters return their argument values for backward compatibility to when Pixel was a Struct class.

Parameters:

  • v (Numeric)

    the alpha value

Returns:

  • (Numeric)

    the given alpha value



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'ext/RMagick/rmpixel.c', line 195

VALUE
Pixel_alpha_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
#if defined(IMAGEMAGICK_7)
    pixel->alpha = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->alpha);
#else
    pixel->opacity = QuantumRange - APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(QuantumRange - pixel->opacity);
#endif
}

#blackNumeric

Get Pixel black value.

Returns:

  • (Numeric)

    the black value



334
335
336
337
338
339
340
341
# File 'ext/RMagick/rmpixel.c', line 334

VALUE
Pixel_black(VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    return INT2NUM(pixel->black);
}

#black=(v) ⇒ Numeric

Set Pixel black value.

  • Pixel is Observable. Setters call #changed, #notify_observers

  • Setters return their argument values for backward compatibility to when Pixel was a Struct class.

Parameters:

  • v (Numeric)

    the black value

Returns:

  • (Numeric)

    the given black value



354
355
356
357
358
359
360
361
362
363
364
365
# File 'ext/RMagick/rmpixel.c', line 354

VALUE
Pixel_black_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->black = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->black);
}

#blueNumeric

Get Pixel blue value.

Returns:

  • (Numeric)

    the blue value



89
90
91
92
93
# File 'ext/RMagick/rmpixel.c', line 89

VALUE
Pixel_blue(VALUE self)
{
    IMPLEMENT_TYPED_ATTR_READER(Pixel, blue, int, &rm_pixel_data_type);
}

#blue=(v) ⇒ Numeric

Set Pixel blue value.

  • Pixel is Observable. Setters call #changed, #notify_observers

  • Setters return their argument values for backward compatibility to when Pixel was a Struct class.

Parameters:

  • v (Numeric)

    the blue value

Returns:

  • (Numeric)

    the given blue value



171
172
173
174
175
176
177
178
179
180
181
182
# File 'ext/RMagick/rmpixel.c', line 171

VALUE
Pixel_blue_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->blue = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM((pixel->blue));
}

#cloneMagick::Pixel

Clone a Pixel.

Returns:

See Also:



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'ext/RMagick/rmpixel.c', line 542

VALUE
Pixel_clone(VALUE self)
{
    VALUE clone;

    clone = Pixel_dup(self);
    if (OBJ_FROZEN(self))
    {
        OBJ_FREEZE(clone);
    }

    RB_GC_GUARD(clone);

    return clone;
}

#cyanNumeric

Get Pixel cyan value.

Returns:

  • (Numeric)

    the cyan value



220
221
222
223
224
225
226
227
# File 'ext/RMagick/rmpixel.c', line 220

VALUE
Pixel_cyan(VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    return INT2NUM(pixel->red);
}

#cyan=(v) ⇒ Numeric

Set Pixel cyan value.

  • Pixel is Observable. Setters call #changed, #notify_observers

  • Setters return their argument values for backward compatibility to when Pixel was a Struct class.

Parameters:

  • v (Numeric)

    the cyan value

Returns:

  • (Numeric)

    the given cyan value



240
241
242
243
244
245
246
247
248
249
250
251
# File 'ext/RMagick/rmpixel.c', line 240

VALUE
Pixel_cyan_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->red = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->red);
}

#dupMagick::Pixel

Duplicate a Pixel.

Returns:

See Also:



566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'ext/RMagick/rmpixel.c', line 566

VALUE
Pixel_dup(VALUE self)
{
    Pixel *pixel;
    VALUE dup;

    pixel = ALLOC(Pixel);
    memset(pixel, '\0', sizeof(Pixel));
    dup = TypedData_Wrap_Struct(CLASS_OF(self), &rm_pixel_data_type, pixel);
    RB_GC_GUARD(dup);

    return rb_funcall(dup, rm_ID_initialize_copy, 1, self);
}

#eql?(other) ⇒ Boolean

Equality. Returns true only if receiver and other are the same object.

Parameters:

  • other (Object)

    the other object

Returns:

  • (Boolean)

    true if other is the same value, otherwise false



587
588
589
590
591
# File 'ext/RMagick/rmpixel.c', line 587

VALUE
Pixel_eql_q(VALUE self, VALUE other)
{
    return NUM2INT(Pixel_spaceship(self, other)) == 0 ? Qtrue : Qfalse;
}

#fcmp(other, fuzz = 0.0, colorspace = Magick::RGBColorspace) ⇒ Boolean

Compare pixel values for equality.

Returns true if equal, otherwise false.

Parameters:

  • other (Magick::Pixel)

    The pixel to which the receiver is compared

  • fuzz (Float) (defaults to: 0.0)

    The amount of fuzz to allow before the colors are considered to be different

  • colorspace (Magick::ColorspaceType) (defaults to: Magick::RGBColorspace)

    The colorspace

Returns:

  • (Boolean)

    true if equal, otherwise false



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'ext/RMagick/rmpixel.c', line 603

VALUE
Pixel_fcmp(int argc, VALUE *argv, VALUE self)
{
    double fuzz = 0.0;
    unsigned int equal;
    ColorspaceType colorspace = RGBColorspace;
    PixelColor this, that;
#if defined(IMAGEMAGICK_6)
    Image *image;
    Info *info;
#endif

    switch (argc)
    {
        case 3:
            VALUE_TO_ENUM(argv[2], colorspace, ColorspaceType);
        case 2:
            fuzz = NUM2DBL(argv[1]);
        case 1:
            // Allow 1 argument
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 to 3)", argc);
            break;
    }

    Color_to_PixelColor(&this, self);
    Color_to_PixelColor(&that, argv[0]);

#if defined(IMAGEMAGICK_7)
    this.fuzz = fuzz;
    this.colorspace = colorspace;
    that.fuzz = fuzz;
    that.colorspace = colorspace;
    equal = IsFuzzyEquivalencePixelInfo(&this, &that);
#else
    // The IsColorSimilar function expects to get the
    // colorspace and fuzz parameters from an Image structure.

    info = CloneImageInfo(NULL);
    if (!info)
    {
        rb_raise(rb_eNoMemError, "not enough memory to continue");
    }

    image = rm_acquire_image(info);

    // Delete Info now in case we have to raise an exception
    DestroyImageInfo(info);

    if (!image)
    {
        rb_raise(rb_eNoMemError, "not enough memory to continue");
    }

    image->colorspace = colorspace;
    image->fuzz = fuzz;

    equal = IsColorSimilar(image, &this, &that);
    DestroyImage(image);
#endif

    return equal ? Qtrue : Qfalse;
}

#greenNumeric

Get Pixel green value.

Returns:

  • (Numeric)

    the green value



78
79
80
81
82
# File 'ext/RMagick/rmpixel.c', line 78

VALUE
Pixel_green(VALUE self)
{
    IMPLEMENT_TYPED_ATTR_READER(Pixel, green, int, &rm_pixel_data_type);
}

#green=(v) ⇒ Numeric

Set Pixel green value.

  • Pixel is Observable. Setters call #changed, #notify_observers

  • Setters return their argument values for backward compatibility to when Pixel was a Struct class.

Parameters:

  • v (Numeric)

    the green value

Returns:

  • (Numeric)

    the given green value



147
148
149
150
151
152
153
154
155
156
157
158
# File 'ext/RMagick/rmpixel.c', line 147

VALUE
Pixel_green_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->green = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM((pixel->green));
}

#hashNumeric

Compute a hash-code.

Returns:

  • (Numeric)

    the hash of self



885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'ext/RMagick/rmpixel.c', line 885

VALUE
Pixel_hash(VALUE self)
{
    Pixel *pixel;
    unsigned int hash;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

    hash  = ScaleQuantumToChar(pixel->red)   << 24;
    hash += ScaleQuantumToChar(pixel->green) << 16;
    hash += ScaleQuantumToChar(pixel->blue)  << 8;
#if defined(IMAGEMAGICK_7)
    hash += ScaleQuantumToChar(pixel->alpha);
#else
    hash += ScaleQuantumToChar(QuantumRange - pixel->opacity);
#endif

    return UINT2NUM(hash >> 1);
}

#initialize_copy(orig) ⇒ Magick::Pixel

Initialize clone, dup methods.

Parameters:

Returns:

See Also:



914
915
916
917
918
919
920
921
922
923
924
925
# File 'ext/RMagick/rmpixel.c', line 914

VALUE
Pixel_init_copy(VALUE self, VALUE orig)
{
    Pixel *copy, *original;

    TypedData_Get_Struct(orig, Pixel, &rm_pixel_data_type, original);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, copy);

    *copy = *original;

    return self;
}

#intensityNumeric

Return the “intensity” of a pixel.

Returns:

  • (Numeric)

    the intensity



993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'ext/RMagick/rmpixel.c', line 993

VALUE
Pixel_intensity(VALUE self)
{
    Pixel *pixel;
    Quantum intensity;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

    intensity = ROUND_TO_QUANTUM((0.299*pixel->red)
                                + (0.587*pixel->green)
                                + (0.114*pixel->blue));

    return QUANTUM2NUM((unsigned long) intensity);
}

#magentaNumeric

Get Pixel magenta value.

Returns:

  • (Numeric)

    the magenta value



258
259
260
261
262
263
264
265
# File 'ext/RMagick/rmpixel.c', line 258

VALUE
Pixel_magenta(VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    return INT2NUM(pixel->green);
}

#magenta=(v) ⇒ Numeric

Set Pixel magenta value.

  • Pixel is Observable. Setters call #changed, #notify_observers

  • Setters return their argument values for backward compatibility to when Pixel was a Struct class.

Parameters:

  • v (Numeric)

    the magenta value

Returns:

  • (Numeric)

    the given magenta value



278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/RMagick/rmpixel.c', line 278

VALUE
Pixel_magenta_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->green = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->green);
}

#marshal_dumpHash

Support Marshal.dump.

Returns:

  • (Hash)

    a representing the dumped pixel



1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'ext/RMagick/rmpixel.c', line 1014

VALUE
Pixel_marshal_dump(VALUE self)
{
    Pixel *pixel;
    VALUE dpixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    dpixel = rb_hash_new();
    rb_hash_aset(dpixel, CSTR2SYM("red"), QUANTUM2NUM(pixel->red));
    rb_hash_aset(dpixel, CSTR2SYM("green"), QUANTUM2NUM(pixel->green));
    rb_hash_aset(dpixel, CSTR2SYM("blue"), QUANTUM2NUM(pixel->blue));
#if defined(IMAGEMAGICK_7)
    rb_hash_aset(dpixel, CSTR2SYM("alpha"), QUANTUM2NUM(pixel->alpha));
#else
    rb_hash_aset(dpixel, CSTR2SYM("opacity"), QUANTUM2NUM(pixel->opacity));
#endif

    RB_GC_GUARD(dpixel);

    return dpixel;
}

#marshal_load(dpixel) ⇒ Object

Support Marshal.load.

Parameters:

  • dpixel (Hash)

    the dumped pixel

Returns:

  • self



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'ext/RMagick/rmpixel.c', line 1043

VALUE
Pixel_marshal_load(VALUE self, VALUE dpixel)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->red = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("red")));
    pixel->green = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("green")));
    pixel->blue = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("blue")));
#if defined(IMAGEMAGICK_7)
    pixel->alpha = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("alpha")));
#else
    pixel->opacity = NUM2QUANTUM(rb_hash_aref(dpixel, CSTR2SYM("opacity")));
#endif
    return self;
}

#redNumeric

Get Pixel red value.

Returns:

  • (Numeric)

    the red value



67
68
69
70
71
# File 'ext/RMagick/rmpixel.c', line 67

VALUE
Pixel_red(VALUE self)
{
    IMPLEMENT_TYPED_ATTR_READER(Pixel, red, int, &rm_pixel_data_type);
}

#red=(v) ⇒ Numeric

Set Pixel red value.

  • Pixel is Observable. Setters call #changed, #notify_observers

  • Setters return their argument values for backward compatibility to when Pixel was a Struct class.

Parameters:

  • v (Numeric)

    the red value

Returns:

  • (Numeric)

    the given red value



123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/RMagick/rmpixel.c', line 123

VALUE
Pixel_red_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->red = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM((pixel->red));
}

#to_color(compliance = Magick::AllCompliance, alpha = false, depth = Magick::MAGICKCORE_QUANTUM_DEPTH, hex = false) ⇒ String

Return the color name corresponding to the pixel values.

Returns the color name as a String.

Parameters:

  • compliance (Magick::ComplianceType) (defaults to: Magick::AllCompliance)

    A ComplianceType constant

  • alpha (Boolean) (defaults to: false)

    If false, the pixel’s alpha attribute is ignored

  • depth (Numeric) (defaults to: Magick::MAGICKCORE_QUANTUM_DEPTH)

    An image depth

  • hex (Boolean) (defaults to: false)

    If true, represent the color name in hex format

Returns:

  • (String)

    the color name as a String



1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
# File 'ext/RMagick/rmpixel.c', line 1199

VALUE
Pixel_to_color(int argc, VALUE *argv, VALUE self)
{
    Info *info;
    Image *image;
    Pixel *pixel;
    MagickPixel mpp;
    MagickBooleanType hex = MagickFalse;
    char name[MaxTextExtent];
    ExceptionInfo *exception;
    ComplianceType compliance = AllCompliance;
    unsigned int alpha = MagickFalse;
    unsigned int depth = MAGICKCORE_QUANTUM_DEPTH;

    switch (argc)
    {
        case 4:
            hex = RTEST(argv[3]);
        case 3:
            depth = NUM2UINT(argv[2]);

            // Ensure depth is appropriate for the way xMagick was compiled.
            switch (depth)
            {
                case 8:
#if MAGICKCORE_QUANTUM_DEPTH == 16 || MAGICKCORE_QUANTUM_DEPTH == 32
                case 16:
#endif
#if MAGICKCORE_QUANTUM_DEPTH == 32
                case 32:
#endif
                    break;
                default:
                    rb_raise(rb_eArgError, "invalid depth (%d)", depth);
                    break;
            }
       case 2:
            alpha = RTEST(argv[1]);
        case 1:
            VALUE_TO_ENUM(argv[0], compliance, ComplianceType);
        case 0:
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 2)", argc);
    }

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

    info = CloneImageInfo(NULL);
    image = rm_acquire_image(info);
    DestroyImageInfo(info);

    if (!image)
    {
        rb_raise(rb_eNoMemError, "not enough memory to continue.");
    }

    exception = AcquireExceptionInfo();

    image->depth = depth;
#if defined(IMAGEMAGICK_7)
    if (alpha)
    {
        image->alpha_trait = BlendPixelTrait;
    }
#else
    image->matte = alpha;
#endif

    rm_init_magickpixel(image, &mpp);
    rm_set_magick_pixel_packet(pixel, &mpp);

    // Support for hex-format color names moved out of QueryMagickColorname
    // in 6.4.1-9. The 'hex' argument was removed as well.
    if (hex)
    {
        if (compliance == XPMCompliance)
        {
#if defined(IMAGEMAGICK_7)
            mpp.alpha_trait = UndefinedPixelTrait;
#else
            mpp.matte = MagickFalse;
#endif
            mpp.depth = (unsigned long) min(1.0 * image->depth, 16.0);
        }
        GetColorTuple(&mpp, MagickTrue, name);
    }
    else
    {
        QueryColorname(image, &mpp, compliance, name, exception);
    }

    DestroyImage(image);
    CHECK_EXCEPTION();
    DestroyExceptionInfo(exception);

    // Always return a string, even if it's ""
    return rb_str_new2(name);
}

#to_hslaArray<Float>

Return [hue, saturation, lightness, alpha] in the same ranges as from_hsla.

Returns:

  • (Array<Float>)

    an array with hsla data

See Also:



1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
# File 'ext/RMagick/rmpixel.c', line 1113

VALUE
Pixel_to_hsla(VALUE self)
{
    double hue, sat, lum, alpha;
    Pixel *pixel;
    VALUE hsla;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);

    ConvertRGBToHSL(pixel->red, pixel->green, pixel->blue, &hue, &sat, &lum);
    hue *= 360.0;
    sat *= 255.0;
    lum *= 255.0;

#if defined(IMAGEMAGICK_7)
    if (pixel->alpha == OpaqueAlpha)
    {
        alpha = 1.0;
    }
    else if (pixel->alpha == TransparentAlpha)
    {
        alpha = 0.0;
    }
    else
    {
        alpha = (double)(pixel->alpha) / (double)QuantumRange;
    }
#else
    if (pixel->opacity == OpaqueOpacity)
    {
        alpha = 1.0;
    }
    else if (pixel->opacity == TransparentOpacity)
    {
        alpha = 0.0;
    }
    else
    {
        alpha = (double)(QuantumRange - pixel->opacity) / (double)QuantumRange;
    }
#endif

    hsla = rb_ary_new3(4, rb_float_new(hue), rb_float_new(sat), rb_float_new(lum), rb_float_new(alpha));

    RB_GC_GUARD(hsla);

    return hsla;
}

#to_sString

Return a string representation of a Magick::Pixel object.

Returns:

  • (String)

    the string



1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
# File 'ext/RMagick/rmpixel.c', line 1305

VALUE
Pixel_to_s(VALUE self)
{
    Pixel *pixel;
    char buff[100];

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    snprintf(buff, sizeof(buff), "red=" QuantumFormat ", green=" QuantumFormat ", blue=" QuantumFormat ", alpha=" QuantumFormat,
            pixel->red, pixel->green, pixel->blue,
#if defined(IMAGEMAGICK_7)
            pixel->alpha);
#else
            (QuantumRange - pixel->opacity));
#endif
    return rb_str_new2(buff);
}

#yellowNumeric

Get Pixel yellow value.

Returns:

  • (Numeric)

    the yellow value



296
297
298
299
300
301
302
303
# File 'ext/RMagick/rmpixel.c', line 296

VALUE
Pixel_yellow(VALUE self)
{
    Pixel *pixel;

    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    return INT2NUM(pixel->blue);
}

#yellow=(v) ⇒ Numeric

Set Pixel yellow value.

  • Pixel is Observable. Setters call #changed, #notify_observers

  • Setters return their argument values for backward compatibility to when Pixel was a Struct class.

Parameters:

  • v (Numeric)

    the yellow value

Returns:

  • (Numeric)

    the given yellow value



316
317
318
319
320
321
322
323
324
325
326
327
# File 'ext/RMagick/rmpixel.c', line 316

VALUE
Pixel_yellow_eq(VALUE self, VALUE v)
{
    Pixel *pixel;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Pixel, &rm_pixel_data_type, pixel);
    pixel->blue = APP2QUANTUM(v);
    rb_funcall(self, rm_ID_changed, 0);
    rb_funcall(self, rm_ID_notify_observers, 1, self);
    return QUANTUM2NUM(pixel->blue);
}