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



914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'ext/RMagick/rmpixel.c', line 914

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

    Data_Get_Struct(self, Pixel, 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:



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'ext/RMagick/rmpixel.c', line 657

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:



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
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
# File 'ext/RMagick/rmpixel.c', line 693

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



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
# File 'ext/RMagick/rmpixel.c', line 1043

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

    Data_Get_Struct(self, Pixel, this);
    Data_Get_Struct(other, Pixel, 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



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'ext/RMagick/rmpixel.c', line 488

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

        Data_Get_Struct(self, Pixel, this);
        Data_Get_Struct(other, Pixel, 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



76
77
78
79
80
81
82
83
84
85
86
# File 'ext/RMagick/rmpixel.c', line 76

VALUE
Pixel_alpha(VALUE self)
{
    Pixel *pixel;
    Data_Get_Struct(self, Pixel, 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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/RMagick/rmpixel.c', line 171

VALUE
Pixel_alpha_eq(VALUE self, VALUE v)
{
    Pixel *pixel;
 
    rb_check_frozen(self);
    Data_Get_Struct(self, Pixel, 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



310
311
312
313
314
315
316
317
# File 'ext/RMagick/rmpixel.c', line 310

VALUE
Pixel_black(VALUE self)
{
    Pixel *pixel;

    Data_Get_Struct(self, Pixel, 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



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

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

    rb_check_frozen(self);
    Data_Get_Struct(self, Pixel, 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



65
66
67
68
69
# File 'ext/RMagick/rmpixel.c', line 65

VALUE
Pixel_blue(VALUE self)
{
    IMPLEMENT_ATTR_READER(Pixel, blue, int);
}

#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



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

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

    rb_check_frozen(self);
    Data_Get_Struct(self, Pixel, 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:



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

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



196
197
198
199
200
201
202
203
# File 'ext/RMagick/rmpixel.c', line 196

VALUE
Pixel_cyan(VALUE self)
{
    Pixel *pixel;

    Data_Get_Struct(self, Pixel, 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



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

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

    rb_check_frozen(self);
    Data_Get_Struct(self, Pixel, 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:



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

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

    pixel = ALLOC(Pixel);
    memset(pixel, '\0', sizeof(Pixel));
    dup = Data_Wrap_Struct(CLASS_OF(self), NULL, destroy_Pixel, 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



563
564
565
566
567
# File 'ext/RMagick/rmpixel.c', line 563

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



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
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
# File 'ext/RMagick/rmpixel.c', line 579

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



54
55
56
57
58
# File 'ext/RMagick/rmpixel.c', line 54

VALUE
Pixel_green(VALUE self)
{
    IMPLEMENT_ATTR_READER(Pixel, green, int);
}

#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



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

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

    rb_check_frozen(self);
    Data_Get_Struct(self, Pixel, 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



861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
# File 'ext/RMagick/rmpixel.c', line 861

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

    Data_Get_Struct(self, Pixel, 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:



890
891
892
893
894
895
896
897
898
899
900
901
# File 'ext/RMagick/rmpixel.c', line 890

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

    Data_Get_Struct(orig, Pixel, original);
    Data_Get_Struct(self, Pixel, copy);

    *copy = *original;

    return self;
}

#intensityNumeric

Return the “intensity” of a pixel.

Returns:

  • (Numeric)

    the intensity



969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'ext/RMagick/rmpixel.c', line 969

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

    Data_Get_Struct(self, Pixel, 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



234
235
236
237
238
239
240
241
# File 'ext/RMagick/rmpixel.c', line 234

VALUE
Pixel_magenta(VALUE self)
{
    Pixel *pixel;

    Data_Get_Struct(self, Pixel, 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



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

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

    rb_check_frozen(self);
    Data_Get_Struct(self, Pixel, 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



990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
# File 'ext/RMagick/rmpixel.c', line 990

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

    Data_Get_Struct(self, Pixel, 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



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

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

    Data_Get_Struct(self, Pixel, 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



43
44
45
46
47
# File 'ext/RMagick/rmpixel.c', line 43

VALUE
Pixel_red(VALUE self)
{
    IMPLEMENT_ATTR_READER(Pixel, red, int);
}

#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



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

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

    rb_check_frozen(self);
    Data_Get_Struct(self, Pixel, 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



1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
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
# File 'ext/RMagick/rmpixel.c', line 1175

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);
    }

    Data_Get_Struct(self, Pixel, 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:



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'ext/RMagick/rmpixel.c', line 1089

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

    Data_Get_Struct(self, Pixel, 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



1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
# File 'ext/RMagick/rmpixel.c', line 1281

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

    Data_Get_Struct(self, Pixel, 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



272
273
274
275
276
277
278
279
# File 'ext/RMagick/rmpixel.c', line 272

VALUE
Pixel_yellow(VALUE self)
{
    Pixel *pixel;

    Data_Get_Struct(self, Pixel, 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



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

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

    rb_check_frozen(self);
    Data_Get_Struct(self, Pixel, 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);
}