Contrasting colours

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Swift

    Contrasting colours

    Imagine you're in a game where your opponent picks the background colour
    of your webpage, in #RRGGBB format, and you have to pick the foreground
    text colour that contrasts best with their choice. I realise that this
    task is almost impossible given various forms of colour-blindness, but
    my target audience is unlikely to be colour-blind (bomb disposal people
    don't last long if they cannot tell live from neutral) so we can assume
    perfect vision.

    If they pick #000000 then of course your text would be #FFFFFF (and vice
    versa).

    If they picked #FF0000 then the choice is less obvious. #00FFFF seems to
    offer the best "mathematic al" contrast, but in tests, (to my eyes)
    #FFFFFF is a better contrast. See http://swiftys.org.uk/contrast.html
    where I am playing with this concept.

    If they pick a background of #7F7F7F then yellow (FFFF00) or cyan
    (00FFFF) offer a good contrast, but magenta (FF00FF) is truly horrible.
    This may be an artefact either of my display, or my use of TrueType, but
    my HTML cannot possibly know about either of these facts.

    Can anyone offer an algorithm that would do a reasonable job faced with
    an arbitrary background colour? I'd hate to end up with a lookup table
    containing 16,777,216 rows!

    --
    Steve Swift


  • EnigmaticSource

    #2
    Re: Contrasting colours

    Try looking at some contrast formulas, and possibly try writing a
    binary search algorithm




    Here's a bit of code, it's reasonably fast, and you should be able to
    adapt it to your needs (if you know php). The Variable $target is
    your target contrast value, 500 is considered pretty good, but that
    isn't always reachable (or not in a time friendly way). if it finds a
    500 or better combination it quits, or it quits when it has come
    pretty close. Note: this will not always give the optimal color, but
    it's close enough, the following returns a color of contrast value
    400, which is 80% of optimal.

    I'm sure it could be better, as I wrote it just for your post, but if
    you decide to use it, it'd be nice, but not necessary to give credit.

    <?php
    /*
    Color Selection By Contrast
    Mark C. Roduner, Jr.
    */
    function color_contrast( $color_1, $color_2) {
    $red_1 = ($color_1 & 0xff0000) >16;
    $red_2 = ($color_2 & 0xff0000) >16;
    $grn_1 = ($color_1 & 0xff00) >8;
    $grn_2 = ($color_2 & 0xff00) >8;
    $blu_1 = ($color_1 & 0xff);
    $blu_2 = ($color_2 & 0xff);
    return (abs($red_1 - $red_2) + abs($grn_1 - $grn_2) + abs($blu_1 -
    $blu_2));
    }

    $source = 0xff00ff;
    $seeker = $marker = 0xffffff;
    $target = 500;
    $best_contrast = 0;
    $best_color = 0;

    $contrast = color_contrast( $source, $seeker);
    while(($contras t < $target) and ($marker != 0)) {
    $marker = intval($marker / 2);
    if($contrast $target) {
    $seeker -= $marker;
    } else if($contrast < $target) {
    $seeker += $marker;
    }
    $contrast = color_contrast( $source, $seeker);
    if($contrast $best_contrast) {
    $best_contrast = $contrast;
    $best_color = $seeker;
    }
    }
    echo("\n Color: " . dechex($best_co lor) . " Contrast $best_contrast" );
    ?>

    On Oct 5, 3:07 am, Steve Swift <Steve.J.Sw...@ gmail.comwrote:
    Imagine you're in a game where your opponent picks the background colour
    of your webpage, in #RRGGBB format, and you have to pick the foreground
    text colour that contrasts best with their choice. I realise that this
    task is almost impossible given various forms of colour-blindness, but
    my target audience is unlikely to be colour-blind (bomb disposal people
    don't last long if they cannot tell live from neutral) so we can assume
    perfect vision.
    >
    If they pick #000000 then of course your text would be #FFFFFF (and vice
    versa).
    >
    If they picked #FF0000 then the choice is less obvious. #00FFFF seems to
    offer the best "mathematic al" contrast, but in tests, (to my eyes)
    #FFFFFF is a better contrast. Seehttp://swiftys.org.uk/contrast.html
    where I am playing with this concept.
    >
    If they pick a background of #7F7F7F then yellow (FFFF00) or cyan
    (00FFFF) offer a good contrast, but magenta (FF00FF) is truly horrible.
    This may be an artefact either of my display, or my use of TrueType, but
    my HTML cannot possibly know about either of these facts.
    >
    Can anyone offer an algorithm that would do a reasonable job faced with
    an arbitrary background colour? I'd hate to end up with a lookup table
    containing 16,777,216 rows!
    >
    --
    Steve Swifthttp://www.swiftys.org .uk/swifty.htmlhttp ://www.ringers.org .uk

    Comment

    • Andy Dingley

      #3
      Re: Contrasting colours

      On 5 Oct, 04:07, Steve Swift <Steve.J.Sw...@ gmail.comwrote:
      Imagine you're in a game where your opponent picks the background colour
      of your webpage, in #RRGGBB format, and you have to pick the foreground
      text colour that contrasts best with their choice.
      Convert from RGB to HSV, then rotate the hue by 180degrees. Convert
      back to RGB.

      The rest is standard algorithms and web searching.

      Comment

      • William Gill

        #4
        Re: Contrasting colours



        Andy Dingley wrote:
        >
        Convert from RGB to HSV, then rotate the hue by 180degrees. Convert
        back to RGB.
        Sorry Andy, it's not so simple. I tried this, and a few other
        algorithms a while back, and failed. I back-burnered the effort, but if
        I'm missing something, please help me get it straight.

        #EADBD1 (hue 24 deg); rotate to 204 deg == #D1E1EB
        brightness difference == 0
        color difference == 57


        Comment

        • Andy Dingley

          #5
          Re: Contrasting colours

          On 5 Oct, 15:34, William Gill <nore...@exampl e.invalidwrote:
          Sorry Andy, it's not so simple.
          It depends a bit what you menan by "contrast". If you take a
          reasonably strong saturation, then rotating the hue is enough - it's a
          pure chroma-based contrast. If you're using a "pastel" shade and you
          want the strongest perceived contrast, then you'll want to vary the
          saturation too. Read Gombrich or Denning for the relevant theory.

          Comment

          • William Gill

            #6
            Re: Contrasting colours



            Andy Dingley wrote:
            On 5 Oct, 15:34, William Gill <nore...@exampl e.invalidwrote:
            >Sorry Andy, it's not so simple.
            >
            It depends a bit what you menan by "contrast". If you take a
            reasonably strong saturation, then rotating the hue is enough - it's a
            pure chroma-based contrast. If you're using a "pastel" shade and you
            want the strongest perceived contrast, then you'll want to vary the
            saturation too. Read Gombrich or Denning for the relevant theory.
            >
            I read several color theory books, and articles on line. Was trying to
            create a helper tool that allowed me to start with a color (from
            client's logo or such) create a basic scheme, and satisfy w3
            accessibility spec. Failed miserably, and gave up with a headache.

            Will look into the references you cite, thanks.

            Comment

            • André Gillibert

              #7
              Re: Contrasting colours

              Steve Swift wrote:

              If they picked #FF0000 then the choice is less obvious. #00FFFF seems to
              offer the best "mathematic al" contrast, but in tests, (to my eyes)
              #FFFFFF is a better contrast. See http://swiftys.org.uk/contrast.html
              where I am playing with this concept.
              >
              That's because the brightness contrast matters. You've just considered the
              tone contrast.

              Vision Australia is the leading national provider of personalised technology, services and information for anyone experiencing or supporting those with vision loss.

              If they pick a background of #7F7F7F then yellow (FFFF00) or cyan
              (00FFFF) offer a good contrast, but magenta (FF00FF) is truly horrible.
              Same thing.
              This may be an artefact either of my display, or my use of TrueType, but
              my HTML cannot possibly know about either of these facts.
              >
              Can anyone offer an algorithm that would do a reasonable job faced with
              an arbitrary background colour? I'd hate to end up with a lookup table
              containing 16,777,216 rows!
              >
              I realise that this task is almost impossible given various forms of
              colour-
              blindness, but my target audience is unlikely to be colour-blind (bomb
              disposal people don't last long if they cannot tell live from neutral)
              so we can assume perfect vision.
              Brightness contrast is important because our eye isn't a screen-to-RGB
              converter.

              1) Cones spectra overlap.

              Which implies that green photons stimulates red cones too.
              (On the other hand, blue and red are at a larger distance).
              This is especially a problem if the colors are very bright, because
              receptors will be saturated.

              2) On peripheral vision, most receptive cells are rods, which are very
              sensible to brightness, but don't percieve the color tone.

              Not strangely, it's the same foreground/background color pairs that are
              most readable for you and for a half-blind grandma.
              I realise that this task is almost impossible given various forms of
              colour-
              blindness
              Not if you just think about ONE color blindness: achromatopsy. It regroups
              all others.
              Just make the text legible when it's converted to gray levels, and it'll
              be legible for everbody.

              --
              If you've a question that doesn't belong to Usenet, contact me at
              <tabkanDELETETH ISnaz@yahoDELET ETHATo.fr>

              Comment

              • Steve Swift

                #8
                Re: Contrasting colours

                Just make the text legible when it's converted to gray levels, and it'll
                be legible for everbody.
                This was the consensus in my employers newsgroups as well (I like to get
                two different sets of opinions, the variations are often enlightening in
                themselves).

                My plan is to convert the background colour to a grey-shade (simply by
                averaging the red/green/blue components initially, then setting the text
                to either black or white depending on the result.

                --
                Steve Swift


                Comment

                Working...