ProfileWell, I thought it was i...PhotosBlogListsMore Tools Help
    September 29

    Two colors red

     If you take a quick look here you’ll have read an interesting article about “well known values” of common types. One example Abhinaba gives is Color.Red and new Color(255,0,0), which he says are “the same”. Moving swiftly over the fact that he meant Color.Red and Color.FromArgb(255,0,0), what about that equivalence claim?

    Well, I suppose it depends how you define equivalence. If you use either of those colors to construct a  SolidBrush and paint a nice big rectangle on the screen, then they’ll both look the same. So, in that sense they are “the same”.

    But try running this little console application, and examine the output.

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Drawing;

     

    namespace TwoColorsRed

    {

        class Program

        {

            static void Main(string[] args)

            {

                Color red1 = Color.Red;

                Color red2 = Color.FromArgb(255, 0, 0);

                //Color red2 = Color.FromName("Red");

     

                if (red1 == red2 )

                {

                    Console.WriteLine(

                        String.Format("Red1({0},{1},{2},{3}) and Red2({4},{5},{6},{7}) " +

                                      "are the same...the Universe is safe",

                        red1.A, red1.R, red1.G, red1.B, red2.A, red2.R, red2.G, red2.B)

                        );

                }

                else

                {

                    Console.WriteLine(

                        String.Format("Red1({0},{1},{2},{3}) and Red2({4},{5},{6},{7}) " + 

                                      "are not the same...call the ChromaCops",

                        red1.A, red1.R, red1.G, red1.B, red2.A, red2.R, red2.G, red2.B)

                        );

                }

                Console.ReadKey();

            }

        }

    }

     

    Delve into Color.Equals() or Color.op_Equality() (operator ==) and you’ll find that things are not quite so simple. In fact, both of these methods of determining equivalence also check other properties.

    First, there’s the state of the color. Did you know that not all colors are actually useful? If we pass an unknown name to Color.FromName(), then you still get a Color object back – it just isn’t valid.

    Then, it checks whether the colors in question are well-known or not, and also compares by Name (which is a read-only property, so you have no real control over this).

    So, when it comes to our first example Color.Red is a known color, but Color.FromArgb(255,0,0) is not; and anyway, their names don’t match either.

    So, why would I care? Red is red, isn’t it? Well – there are often good reasons for trying to compare two colors – trying to find a match in a fixed palette, or looking for similar pixels in a bitmap region, for example. You need to check the (A)RGB values by hand, rather than using the seemingly-handy comparison functions.

    This is a more general issue, both for API designers and consumers.

    From the API designer’s point of view it is recommended that, if you are providing equality overrides, that your comparator should perform an identity-like compare. The Color.Red and your Color.FromArgb(255,0,0) are clearly not the same color, in an identity sense – they’ve got different names for a start!

    However, from a consumer’s point of view, you often don’t want an “identity” comparison – especially with value types. More often than not, you need a semantic comparison appropriate to your application (like comparing the color values). And you have to write that yourself.

     If it is a very common comparison to perform, then you might be tempted to ask the API designer to roll it into the class concerned (especially if you are the API designer, too), but you should be very careful before deciding to do so. You don’t want to confuse the situation further by adding an army of subtly different comparison functions to a simple class or struct – especially one like Color.

    Tricky fellow, equality.

    Comments (6)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    http://www.adapterlist.com/dell/xd187.htm New Dell Inspiron B130 1300 b120 Battery 312-0416 56whr laptop battery ,
    http://www.adapterlist.com/gateway/12msbg.htm Gateway 8msb 8msbg 12msbg laptop battery ,
    http://www.adapterlist.com/gateway/4s2p.htm Gateway 11.1v 4800mah M360 M460 M680 battery laptop battery ,
    http://www.adapterlist.com/gateway/8msbg.htm Gateway 11.1v 5.2ah M360 M460 M680 battery laptop battery ,
    http://www.adapterlist.com/gateway/m320.htm GATEWAY BATTERY 6500922 m320 m325 4000mx laptop battery ,
    http://www.adapterlist.com/gateway/m680.htm Gateway M360 M460 M680 8-Cell Notebook Battery 6500949 laptop battery ,
    http://www.adapterlist.com/gateway/s62066l.htm GATEWAY BATTERY s62066l m320 m325 4000mx laptop battery ,
    http://www.adapterlist.com/gateway/squ-412.htm Gateway 11.1v 4400mah M360 M460 M680 battery laptop battery ,
    http://www.adapterlist.com/gateway/4ur18650f-2-qc-ea1.htm 4ur18650f-2-qc-ea1,6500632,6500665 laptop battery ,
    http://www.adapterlist.com/gateway/6500358.htm battery for solo 9300 6500358 GT-6300L laptop battery ,
    http://www.adapterlist.com/gateway/bat0016.htm gateway bat0016 Motion M1200 M1300 laptop battery ,
    http://www.adapterlist.com/gateway/btp-68b3.htm Battery for Gateway Solo M500 M505 Medion MD2900 MD6179 laptop battery ,
    http://www.adapterlist.com/hp/320912-001.htm HP Compaq Battery for NC8200 nc8230 7400 laptop battery ,
    http://www.adapterlist.com/hp/336962-001-002-battery.htm Battery fits HP Pavilion zt3000 zt3010US 337607-001 laptop battery ,
    http://www.adapterlist.com/hp/364602-001.htm New Battery For Compaq NX6120 NC6100 NC6120 NX6100 NC6200 laptop battery ,
    http://www.adapterlist.com/hp/dv6000.htm New OEM HP HSTNN-DB42 DV2000 DV6000 V3000 V6000 laptop battery ,
    http://www.adapterlist.com/hp/dv9000.htm New OEM HP HSTNN-LB33 dv9000 DV9000 dv9600 laptop battery ,
    http://www.adapterlist.com/hp/hp510.htm laptop battery for hp 510 530 HSTNN-FB40 laptop battery ,
    http://www.adapterlist.com/hp/hp520.htm NEW Battery for HP 500 HSTNN-FB39 434045-661 laptop battery ,
    http://www.adapterlist.com/hp/hstnn-c16c.htm Battery fits HP Pavilion DV8000 DV8100 DV8200 laptop battery ,
    http://www.adapterlist.com/hp/hstnn-db17.htm New Battery for HP M2000 Series DV1000 DV4000 laptop battery ,
    http://www.adapterlist.com/hp/nc2400-battery.htm New Compaq 2510p NC2400 Laptop Extended Battery EH768AA laptop battery ,
    http://www.adapterlist.com/hp/nc4000.htm HP NC4010 DD880A NC4000 laptop battery ,
    http://www.adapterlist.com/hp/nc6000.htm battery for Compaq/HP NX5000 NC6000 NC8000 DG105A 4.4A laptop battery ,
    http://www.adapterlist.com/hp/zt1000.htm New Battery For HP Pavilion ZT1000 F2299A F3172B F3172A laptop battery ,
    http://www.adapterlist.com/sony/pcga-bp2sa-002-battery.htm grey Battery For Sony PCGA-BP2S PCGA-BP2SA VAIO PCG-SR17 NEW laptop battery ,
    http://www.adapterlist.com/sony/pcga-bp2sa-battery.htm Battery For Sony PCGA-BP2S PCGA-BP2SA VAIO PCG-SR17 NEW black laptop battery ,
    http://www.adapterlist.com/sony/pcga-bp2t.htm battery fits sony PCGA-BP2T PCGA-BP3T PCG-TR1/B PCG-TR2 laptop battery ,
    http://www.adapterlist.com/sony/pcga-bp2v.htm laptop Battery for Sony PCGA-BP2V laptop battery ,
    http://www.adapterlist.com/sony/pcg-gr100.htm Original SONY pcga-bp2e vgp-bp2ea pcg-gr100 Battery laptop battery ,
    http://www.adapterlist.com/sony/vgp-bps2a.htm LAPTOP BATTERY SONY VAIO VGP-BPS2 VGP-BPS2B VGP-BPS2A black laptop battery ,
    http://www.adapterlist.com/sony/vgp-bps2a-002-battery.htm 7.2ah LAPTOP BATTERY SONY VAIO VGP-BPS2 VGP-BPS2B VGP-BPS2A black laptop battery ,
    http://www.adapterlist.com/toshiba/pa3098u-1bas-battery.htm Battery For TOSHIBA Satellite 1200 3000 3005 PA3098U laptop battery ,
    http://www.adapterlist.com/toshiba/pa3209u-1brs.htm Battery for Toshiba Satellite 1100 1110 PA3209U-1BRS laptop battery ,
    http://www.adapterlist.com/toshiba/pa3210u-battery.htm NEW Toshiba Satellite 1115 4500mAh Battery PA3210U laptop battery ,
    http://www.adapterlist.com/toshiba/pa3291u-battery.htm New battery for TOSHIBA P20 P25 PA3291U-1BRS 6600mAh laptop battery ,
    http://www.adapterlist.com/toshiba/pa3399u.htm battery fits TOSHIBA PA3399U-1BAS PA3399U-1BRS 6600mAH laptop battery ,
    http://www.adapterlist.com/toshiba/pa3399u-1bas.htm Battery for Toshiba PA3399U-1BRS PA3399U-2BAS 1BAS laptop battery ,
    http://www.adapterlist.com/toshiba/pa3356u-1brs.htm Toshiba PA3356U-3BAS PA3356U-3BRS PA3456U-1BRS Battery laptop battery ,
    http://www.adapterlist.com/toshiba/pa3356u-1brs-002-battery.htm 8.8ah Battery TOSHIBA PA3356U-1BAS PA3356U-1BRS PA3356U-2BRS laptop battery ,
    http://www.adapterlist.com/toshiba/pa3420u.htm toshiba pa3420u-1brs l10 l15 l20 laptop battery ,
    http://www.adapterlist.com/toshiba/pa3421u-1brs.htm Battery for Toshiba M30X M35X M40X PA3395U-1BRS PA3421U laptop battery ,
    http://www.adapterlist.com/toshiba/pa3431u-1brs.htm TOSHIBA SATELLITE M65 BATTERY PA3431U-BRS PABAS068 laptop battery ,
    http://www.adapterlist.com/toshiba/pa3451u.htm TOSHIBA PA3451U-1BRS PABAS067 laptop battery ,
    http://www.adapterlist.com/toshiba/pa3465u.htm Genuine TOSHIBA PA3465U-1BRS Laptop Battery PABAS069 laptop battery ,
    Mar. 25
    Nov. 28
    Picture of Anonymous
    (没有名字) wrote:
    Fallout 3 makes the list purely because it looks like Bethesda has this one cooking as a pet project.wow gold All I can say is that from reading their development blogs and seeing screenshots of the ongoing project, this looks like a game that has been made from the ground up for the real fans of the Fallout series. While it is a departure from the three-quarter perspective, turn-based adventure that was the first two games, released 10 years ago, it looks like this game will stay completely faithful to the post-apocalyptic environment and quirky dialog that made the series so great. The contest they held for fans to be able to create a perk for the game just shows how much they care about the community created with the first two games.
    Nov. 19
    Oct. 27
    No namewrote:
    Welcome to enter (wow gold) and (wow power leveling) trading site, (wow gold) are cheap, (wow power leveling) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions! -278096917762261
    Aug. 20
    July 24

    Trackbacks (4)

    The trackback URL for this entry is:
    http://mwadams.spaces.live.com/blog/cns!652A0FB566F633D5!1832.trak
    Weblogs that reference this entry