# Convert RGB color to websafe

Web safe colors are in intervals of 51, they include 0, 3, 6, 9, c, and, f as digits in hex (such as #993333), or 0, 51, 102, 153, 204, 255 in decimal. Your task today is convert a given color to a web safe one. You may read more about web safe colors on Wikipedia.

Input

You will take in 3 integers between 0, 255 inclusive representing red, green, and blue values.

Output

Output the converted 3 integers, as an array or similar structure.

Rules

• answers must be rounded to the nearest value, not floor or ceiling. (25 -> 0 and 26 -> 51)
• As this is code golf, lowest bytes wins.

Test Cases

Input            Output
(0, 25, 76)      (0, 0, 51)
(1, 26, 77)      (0, 51, 102)
(127, 178, 229)  (102, 153, 204)
(128, 179, 230)  (153, 204, 255)
(0, 254, 255)    (0, 255, 255)
(9, 143, 53)     (0, 153, 51)


I'm sure these answers will be pretty short; this is an easier question meant for those more novice at code golf, however feel free to answer regardless of your skill/experience.

• Do you want output as the hex form, or RGB form? And which form is the input? Nov 1 '17 at 0:05
• Could you please clarify your explanation and add some input/output examples? For future reference, the Sandbox is a very good place to receive feedback on challenges before posting them on the main site
– Leo
Nov 1 '17 at 0:09
• Also, how do we round? Just floor? Half up? Nov 1 '17 at 0:17
– tsh
Nov 1 '17 at 2:41

# Japt, 4 bytes

mr51


Try it online!

Input and output is a 3-item array.

Just maps each item rounded to the nearest multiple of 51.

# Taxi, 1015 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Tom's Trims.Pickup a passenger going to Tom's Trims.Pickup a passenger going to Tom's Trims.[a]Go to Tom's Trims:n.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Divide and Conquer.51 is waiting at Starchild Numerology.51 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 l 1 l 1 l 2 l.Pickup a passenger going to Divide and Conquer.Pickup a passenger going to Multiplication Station.Go to Divide and Conquer:w 1 r 3 r 1 r 2 r 1 r.Pickup a passenger going to Rounders Pub.Go to Rounders Pub:e 1 r 1 r 1 l.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:n 1 r 1 r 1 l 1 r.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 r 1 l.Pickup a passenger going to Post Office.' ' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 1 l 2 l.Pickup a passenger going to Post Office.Go to Post Office:n 1 r 2 r 1 l.Switch to plan "a".


Try it online!

Un-golfed:

Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to Tom's Trims.
Pickup a passenger going to Tom's Trims.
Pickup a passenger going to Tom's Trims.
[a]
Go to Tom's Trims: north.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to Divide and Conquer.
51 is waiting at Starchild Numerology.
51 is waiting at Starchild Numerology.
Go to Starchild Numerology: north 1st left 1st left 1st left 2nd left.
Pickup a passenger going to Divide and Conquer.
Pickup a passenger going to Multiplication Station.
Go to Divide and Conquer: west 1st right 3rd right 1st right 2nd right 1st right.
Pickup a passenger going to Rounders Pub.
Go to Rounders Pub: east 1st right 1st right 1st left.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station: north 1st right 1st right 1st left 1st right.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st right 1st left.
Pickup a passenger going to Post Office.
' ' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 1st left 2nd left.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st right 2nd right 1st left.
Switch to plan "a".


You can't detect if someone is waiting at Post Office so you start by picking up the three passengers and moving them. Divide each by 51, round, multiply by 51, and output.

# Python 2, 33 bytes

lambda c:[(n+25)/51*51for n in c]


Try it online!

# MATLAB/Octave, 18 13 bytes

@(x)51*(x/51)


Try it online!

Takes an array of type uint8 with three values (R,G,B), and returns a uint8 array of web-safe (R,G,B). MATLAB/Octave use uint8 types to represent colours in images, so it's not really cheating to expect the same.

# Javascript (ES6), 28 27 bytes

-1 byte thanks to @Arnauld

a=>a.map(n=>51*(0|.5+n/51))


Takes input as an array ([red, green, blue]) and returns the output in the same format.

f=a=>a.map(n=>51*(0|.5+n/51))
<div oninput="o.innerText=f([r.value,g.value,b.value])"><input id=r type=number value=0 /><input id=g type=number value=0 /><input id=b type=number value=0 /><pre id=o>

• (n*5+130>>8)*51 would also work. But it's (now) 1 byte longer and I can't figure out a way to golf it some more. Nov 1 '17 at 20:22

# Jelly, 9 8 bytes

Byte saved thanks to Dennis

51Ɠ÷+.Ḟ×


Try it online!

• I forgot to specify earlier that it must be rounded to the nearest, not floor or ceiling. I updated the question description and I'm sorry I didn't mention this earlier. Nov 1 '17 at 0:26

# Perl 5, 23 18 + 1 (-n) = 19 bytes

say 51*int$_/51+.5  Try it online! -4 bytes thanks to @Dennis # Mathematica, 24 bytes Nearest[51Range[0,5],#]&  Try it online! -2 bytes from @MishaLavrov • Nearest[51Range[0,5],#]& saves two bytes. Also, I think adding Tr/@ at the beginning would be neater, returning output like {153, 204, 255} instead of {{153}, {204}, {255}}. Nov 1 '17 at 16:25 • @MishaLavrov "output as an array or similar structure". why waste 4 bytes? this is code-golf Nov 1 '17 at 18:54 • I guess that's fair. Nov 1 '17 at 20:13 • @NoOneIsHere No,because it doesn't work. You should have checked this yourself in TIO link Nov 3 '17 at 22:32 # Excel VBA, 26 Bytes Anonymous VBE immediate window function that takes input as integers from range [A1:C1] and outputs the corresponding safeview values to range [A2:C2]. [A2:C2]="=51*INT(.5+A1/51)  ### Sample Output # Python 2, 3736 33 bytes lambda c:[(n+25)/51*51for n in c]  Try it online! # Python 3, 3635 34 bytes lambda c:[(n+25)//51*51for n in c]  Try it online! • For both Python 2 and Python 3 there's a round builtin that can save you 1 byte. Nov 1 '17 at 11:48 • @EriktheOutgolfer Thanks, for some reason my round-version was longer... Nov 1 '17 at 11:54 • You can save more bytes by changing your math a bit Try it online! – Leo Nov 1 '17 at 20:23 # PHP, 42 bytes while($k++<3)echo($argv[$k]/51+.5|0)*51,_;


Run with -nr or try it online.

## Batch, 50 bytes

@for %%i in (%*)do @cmd/cset/a(%%i+25)/51*51&echo(


Takes input as command-line parameters.

# Retina, 53 bytes


25;
\d+
$* ; 1{51} a 1 a 51, 51,$*
(?<= )1*
\$.&


Try it online!

Every number in the input has a leading space

## C++, 67 bytes

-3 bytes thanks to Zacharý and ceilingcat

#define R(t)t=(t+25)/51*51;
void s(int&r,int&g,int&b){R(r)R(g)R(b)}


Return by parameters

Code to test ( include required : iostream, vector and tuple ) :

std::vector<std::pair<tuple3int, tuple3int>> testList{
{ { 0,25,76 },{ 0,0,51 } },
{ { 1,26,77 },{ 0,51,102 } },
{ { 127,178,229 },{ 102,153,204 } },
{ { 128,179,230 },{ 153,204,255 } },
{ { 0,254,255 },{ 0,255,255 } },
{ { 9,143,53 },{ 0,153,51 } }
};

for (auto& a : testList) {
s(std::get<0>(a.first), std::get<1>(a.first), std::get<2>(a.first));
if (a.first != a.second)
std::cout << "Error\n";
}

• I also think it might be possibe to change t=((t+25)/51)*51 to t=(t+25)/51*51, in addition to removing the space between R(t) and t as @ceilingcat said. Nov 3 '17 at 14:10

# Kotlin, 111 bytes

fun w(r:Int,g:Int,b:Int):Array<Int>(){return arrayOf(Math.rint(r/51)*51,Math.rint(g/51)*51,Math.rint(b/51)*51)}

• Welcome to PPCG! As it seems, the code provided is 117 bytes long. We usually need the fully golfed code (i.e. all unnecessary whitespace removed). In case you haven't heard, there is an online environment to execute various programming languages, called Try it online! You may find it beneficial to add this to your answer. Nov 7 '17 at 18:55
• @ConorO'Brien Looks like the whitespace chars were not counted; they're 6 bytes in total here. However, I'd highly recommend adding a notice such as "spaced added for clarity; not counted" if that's the case indeed. Nov 7 '17 at 19:00
• Edited. 2 whitespace characters are necessary for the keywords Nov 7 '17 at 19:03
• This looks like it's only 111 bytes long. The 6 bytes I was referring to were the unnecessary ones. Nov 7 '17 at 19:05

# q/kdb+, 11 bytes

Solution:

51 xbar 25+


Example:

/ apply across test cases and then cut into chunks of 3
q)3 cut 51 xbar 25+0 25 76 1 26 77 127 178 229 128 179 230 0 254 255 9 143 53
0   0   51
0   51  102
102 153 204
153 204 255
0   255 255
0   153 51


Explanation:

Nice and easy using xbar built-in. Q is interpreted right-to-left:

51 xbar 25+ / the solution
25+ / vectorised add (add 25 to each item on the right)
51 xbar     / round down to nearest multiple of 51


# C# (.NET Core), 28 bytes

a=>a.Select(n=>(n+25)/51*51)


Try it online!

Same solution as others. I didnt count headers (system and linq). You can if you want. :)

# Funky, 33 bytes

l=>l::map(z=>math.round(z/51)*51)


Try it online!