ColourGuidelines

From Custard
Jump to: navigation, search


Suggest colours within colour guidelines

According to the BBC Colour guidelines spec. http://www.bbc.co.uk/guidelines/futuremedia/accessibility/colour_contrast.shtml

And using formulas from http://juicystudio.com/services/colourcontrast.php

Perl code:

Set your sample colour in the constants in the program:


#!/usr/bin/perl

=pod

From http://juicystudio.com/services/colourcontrast.php

Brightness:
((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000
threshold = 125;

Colour difference

(maximum (Red value 1, Red value 2) - minimum (Red value 1, Red value 2)) + (maximum (Green value 1,
Green value 2) - minimum (Green value 1, Green value 2)) + (maximum (Blue value 1, Blue value 2) - mi
nimum (Blue value 1, Blue value 2))

threshold = 500

=cut

my $brightness_threshold = 125;
my $difference_threshold = 400;   # Beeb spec says 400

my $sample_red = 255;
my $sample_green = 255;
my $sample_blue = 255;

my $sample_brightness = brightness( $sample_red, $sample_green, $sample_blue );

my @possible;
for (my $red=0; $red<=255; $red+=16 ) {
  for (my $green=0; $green<=255; $green+=16 ) {
    for (my $blue=0; $blue<=255; $blue+=16 ) {
      my $brightness =  brightness( $red, $green, $blue );
      my $difference = colour_difference( $sample_red,$sample_green, $sample_blue, $red,$green,$blue
);
      if ((abs( $sample_brightness - $brightness) >= $brightness_threshold) &&
         ($difference >= $difference_threshold)) {
        push @possible, [$red,$green,$blue];
      }
    }
  }
}

for my $possible (@possible) {
  printf( "#%02x%02x%02x\n",@{$possible} );
}
exit(0);

sub brightness {
  my ($red,$green,$blue) = @_;
  return (($red * 299) + ($green * 587) + ($blue * 114)) / 1000;
}

sub colour_difference {
  my ($red_1,$green_1,$blue_1, $red_2,$green_2,$blue_2) = @_;

  (max($red_1,$red_2) - min($red_1,$red_2)) +
  (max($green_1,$green_2) - min($green_1,$green_2)) +
  (max($blue_1,$blue_2) - min($blue_1,$blue_2));
}

sub max {
  ($_[0] > $_[1]) ? $_[0] : $_[1];
}

sub min {
  ($_[0] < $_[1]) ? $_[0] : $_[1];
}

Sample output:

{| border="1"
|-
[eschaton]$ perl ~/tmp/colour.pl ||head -25
|}

#000000
#000010
#000020
#000030
#000040
#000050
#000060
#000070
#000080
#000090
#0000a0
#0000b0
#0000c0
#0000d0
#0000e0
#0000f0
#001000
#001010
#001020
#001030
#001040
#001050
#001060
#001070
#001080
...
...


-- Main.BruceJames - 17 Oct 2008

Personal tools