Wierd one - web color comparison

Posted on 2004-11-19
Last Modified: 2010-03-31
Hi Experts,

         Ive parsed the style sheet of a web page document and now im examining the document to identify visual segnmentation e.g. two sections with distinctly different background colors. What i need to know is if its possiable to compare two strings that represent color in style sheets i.e. background-color:#333333;

I need a method that gives a quantitive value to show how much the colors differ.

Any help welcomed.

Thanks, Chris.
Question by:icb01co1
    LVL 92

    Expert Comment

    not sure i understand your problem.
    Why don't you just parse the value as an integer
    LVL 15

    Accepted Solution

    Yes and no.  The colorcodes used in RGB colors are not linear with respect to the percieved colors.
    So you can compare, but the comparison is not worth much.

    the color #334455 should be read as 3 seperate numbers
      int redComponent    = Integer.parseInt( "#334455".substring(1,3), 16 );
      int greenComponent = Integer.parseInt( "#334455".substring(3,5), 16 );
      int blueComponent   = Integer.parseInt( "#334455".substring(5), 16 );

    to compare you then calculate the difference individually for each color component:
        int redDifference = Math.abs( redComponent_nr1 - redComponent_nr2 );
        int greenDifference = Math.abs( greenComponent_nr1 - greenComponent_nr2 );
        int blueDifference = Math.abs( blueComponent_nr1 - blueComponent_nr2 );
        int totalDifference = redDifference +greenDifference +blueDifference;

    But remember that this difference calculation will not give you quite the differences you intuitively expect. I just hope it may be good enough for your purposes.

    regards JakobA

    Author Comment

    Pure genius, thankyou very much kind sir.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now