Peter’s blog ✴ Week 383 ✴ 20 July 2026

THE WEEKLY CHALLENGE
Similar list, friendly colour

The Perl Camel

Task 2

Nearest RGB

You are given a 6-digit hex color. Write a script to round the RGB channels to the nearest web-safe value and return the nearest web safe RGB colour.

The web-safe colour components are 00 (0), 33 (51), 66 (102), 99 (153), CC (204) and FF (255).

Examples


Example 1
Input: $color = '#F4B2D1'
Output: '#FF99CC'
Red: F4 (Decimal 244), closer to 255 => FF
Green: B2 (Decimal 178), closer to 153 => 99
Blue: D1 (Decimal 209), closer to 204 => CC
So the nearest RGB: '#FF99CC'

Example 2
Input: $color = '#15E6E5'
Output: '#00FFCC'
Red: 15 (Decimal 21), closer to 0 => 00
Green: E6 (Decimal 230), closer to 255 => FF
Blue: E5 (Decimal 229), closer to 204 => CC

Example 3
Input: $color = '#191A65'
Output: '#003366'
Red: 19 (Decimal 25), closer to 0 => 00
Green: 1A (Decimal 26), closer to 51 => 33
Blue: 65 (Decimal 101), closer to 102 => 66

Example 4
Input: $color = '#2D5A1B'
Output: '#336633'
Red: 2D (Decimal 45), closer to 51 => 33
Green: 5A (Decimal 90), closer to 102 => 66
Blue: 1B (Decimal 27), closer to 51 => 33

Example 5
Input: $color = '#00FF66'
Output: '#00FF66'
Red: 00 (Decimal 0), closer to 0 => 00
Green: FF (Decimal 255), closer to 255 => FF
Blue: 66 (Decimal 102), closer to 102 => 66

Analysis

Well, this takes me back. Web-safe colours were a thing for a short period in the 1990s starting with the advent of cheap(ish) colour terminals and ending a few years later when they could cope with what was then called 'millions' of colours - ie hex 000000 to hex FFFFFF.

To show the working I have split the calculation into several lines, but they could equally be combined into one. The decimal value of each colour is incremented by 24, and the resulting number is integer-divided by 51 to give an index in the range 0 - 5. The character at that index of the string 0369CF is then added twice to the web-safe colour string.

It's all pretty well history now.

Try it 

Your input:



eg: ABCDEF

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/383/2

use v5.26;    # The Weekly Challenge - 2026-07-20
use utf8;     # Week 383 - task 2 - Nearest rgb
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

nearest_rgb('F4B2D1');
nearest_rgb('15E6E5');
nearest_rgb('191A65');
nearest_rgb('2D5A1B');
nearest_rgb('00FF66');

sub nearest_rgb {
    
    my ($colour, $value, $safe);
    
    # initialise
    $colour = shift;
    
    # loop over 3 components
    while ($colour =~ m|(..)|g) {
        $value = hex($1);
        $value += 25;
        $value = int($value / 51);
        $value = substr('0369CF', $value, 1);
        $safe .= $value . $value;   
    }

    say qq[\nInput:  '$colour'];
    say qq[Output: '$safe'];
}

11 lines of code

Output from script


Input:  'F4B2D1'
Output: 'FF99CC'

Input:  '15E6E5'
Output: '00FFCC'

Input:  '191A65'
Output: '003366'

Input:  '2D5A1B'
Output: '336633'

Input:  '00FF66'
Output: '00FF66'

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain