4
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ Do you want output as the hex form, or RGB form? And which form is the input? \$\endgroup\$ – ATaco Nov 1 '17 at 0:05
  • \$\begingroup\$ 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 \$\endgroup\$ – Leo Nov 1 '17 at 0:09
  • \$\begingroup\$ Also, how do we round? Just floor? Half up? \$\endgroup\$ – ATaco Nov 1 '17 at 0:17
  • 1
    \$\begingroup\$ I had made some edit. Hopes you don't mind. \$\endgroup\$ – tsh Nov 1 '17 at 2:41

18 Answers 18

8
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

Python 2, 33 bytes

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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>

\$\endgroup\$
  • \$\begingroup\$ (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. \$\endgroup\$ – Arnauld Nov 1 '17 at 20:22
3
\$\begingroup\$

Jelly, 9 8 bytes

Byte saved thanks to Dennis

51Ɠ÷+.Ḟ×

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Justin Nov 1 '17 at 0:26
3
\$\begingroup\$

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

say 51*int$_/51+.5

Try it online!

-4 bytes thanks to @Dennis

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 24 bytes

Nearest[51Range[0,5],#]&

Try it online!

-2 bytes from @MishaLavrov

\$\endgroup\$
  • 1
    \$\begingroup\$ 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}}. \$\endgroup\$ – Misha Lavrov Nov 1 '17 at 16:25
  • \$\begingroup\$ @MishaLavrov "output as an array or similar structure". why waste 4 bytes? this is code-golf \$\endgroup\$ – J42161217 Nov 1 '17 at 18:54
  • \$\begingroup\$ I guess that's fair. \$\endgroup\$ – Misha Lavrov Nov 1 '17 at 20:13
  • \$\begingroup\$ @NoOneIsHere No,because it doesn't work. You should have checked this yourself in TIO link \$\endgroup\$ – J42161217 Nov 3 '17 at 22:32
3
\$\begingroup\$

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

enter image description here

\$\endgroup\$
3
\$\begingroup\$

Python 2, 37 36 33 bytes

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

Try it online!

Python 3, 36 35 34 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ For both Python 2 and Python 3 there's a round builtin that can save you 1 byte. \$\endgroup\$ – Erik the Outgolfer Nov 1 '17 at 11:48
  • \$\begingroup\$ @EriktheOutgolfer Thanks, for some reason my round-version was longer... \$\endgroup\$ – TFeld Nov 1 '17 at 11:54
  • \$\begingroup\$ You can save more bytes by changing your math a bit Try it online! \$\endgroup\$ – Leo Nov 1 '17 at 20:23
2
\$\begingroup\$

PHP, 42 bytes

while($k++<3)echo($argv[$k]/51+.5|0)*51,_;

Run with -nr or try it online.

\$\endgroup\$
2
\$\begingroup\$

Batch, 50 bytes

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

Takes input as command-line parameters.

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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";
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Zacharý Nov 3 '17 at 14:10
2
\$\begingroup\$

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)}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Conor O'Brien Nov 7 '17 at 18:55
  • \$\begingroup\$ @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. \$\endgroup\$ – Erik the Outgolfer Nov 7 '17 at 19:00
  • \$\begingroup\$ Edited. 2 whitespace characters are necessary for the keywords \$\endgroup\$ – Trenton Telge Nov 7 '17 at 19:03
  • \$\begingroup\$ This looks like it's only 111 bytes long. The 6 bytes I was referring to were the unnecessary ones. \$\endgroup\$ – Erik the Outgolfer Nov 7 '17 at 19:05
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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. :)

\$\endgroup\$
1
\$\begingroup\$

Funky, 33 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.