Find the closest three-digit hex colourSolving Mastermind in 6 or less movesBetter Hex Color Codes for Your InternetConvert RGB color to websafeHow to code colors in hexFoam Bath LettersRoboZZle interpreterLet's design a digit mosaicTrue color codeExecute Triangularity MoveGenerate an RGB colour grid
German idiomatic equivalents of 能骗就骗 (if you can cheat, then cheat)
How did they film the Invisible Man being invisible in 1933?
What does 'in attendance' mean on an England death certificate?
How soon after takeoff can you recline your airplane seat?
Reusable spacecraft: why still have fairings detach, instead of open/close?
Can I submit a paper to two or more journals at the same time?
How useful would a hydroelectric power plant be in the post-apocalypse world?
How can an inexperienced GM keep a game fun for experienced players?
Does "boire un jus" tend to mean "coffee" or "juice of fruit"?
Could all three Gorgons turn people to stone, or just Medusa?
How to track mail undetectably?
How far can gerrymandering go?
Why isn't UDP with reliability (implemented at Application layer) a substitute of TCP?
Position representation of spin states and spin operators
Tricolour nonogram
Does a lens with a bigger max. aperture focus faster than a lens with a smaller max. aperture?
Fully submerged water bath for stove top baking?
Does it make sense to (partially) create a conlang that you don't intend to actually use in the story?
Why was Pan Am Flight 103 flying over Lockerbie?
How to find out who created an event?
Why didn't Caesar move against Sextus Pompey immediately after Munda?
Is my guitar action too high or is the bridge too high?
Automorphisms and epimorphisms of finite groups
Russian equivalents of 能骗就骗 (if you can cheat, then cheat)
Find the closest three-digit hex colour
Solving Mastermind in 6 or less movesBetter Hex Color Codes for Your InternetConvert RGB color to websafeHow to code colors in hexFoam Bath LettersRoboZZle interpreterLet's design a digit mosaicTrue color codeExecute Triangularity MoveGenerate an RGB colour grid
$begingroup$
In CSS, colours can be specified by a "hex triplet" - a three byte (six digit) hexadecimal number where each byte represents the red, green, or blue components of the colour. For instance, #FF0000
is completely red, and is equivalent to rgb(255, 0, 0)
.
Colours can also be represented by the shorthand notation which uses three hexadecimal digits. The shorthand expands to the six digit form by duplicating each digit. For instance, #ABC
becomes #AABBCC
.
Since there are fewer digits in the hex shorthand, fewer colours can be represented.
The challenge
Write a program or function that takes a six digit hexadecimal colour code and outputs the closest three-digit colour code.
Here's an example:
- Input hex code: #28a086
- Red component
- 0x28 = 40 (decimal)
- 0x22 = 30
- 0x33 = 51
- 0x22 is closer, so the first digit of the shortened colour code is 2
- Green component
- 0xa0 = 160
- 0x99 = 153
- 0xaa = 170
- 0x99 is closer, so the second digit is 9
- Blue component
- 0x86 = 134
- 0x77 = 119
- 0x88 = 136
- 0x88 is closer, so the third digit is 8
- The shortened colour code is #298 (which expands to #229988)
Your program or function must accept as input a six digit hexadecimal colour code prepended with #
and output a three digit colour code prepended with #
.
Examples
- #FF0000 → #F00
- #00FF00 → #0F0
- #D913C4 → #D1C
- #C0DD39 → #BD3
- #28A086 → #298
- #C0CF6F → #BC7
Scoring
This is a code-golf challenge, so shortest answer in your language wins! Standard rules apply.
code-golf hexadecimal color
$endgroup$
|
show 1 more comment
$begingroup$
In CSS, colours can be specified by a "hex triplet" - a three byte (six digit) hexadecimal number where each byte represents the red, green, or blue components of the colour. For instance, #FF0000
is completely red, and is equivalent to rgb(255, 0, 0)
.
Colours can also be represented by the shorthand notation which uses three hexadecimal digits. The shorthand expands to the six digit form by duplicating each digit. For instance, #ABC
becomes #AABBCC
.
Since there are fewer digits in the hex shorthand, fewer colours can be represented.
The challenge
Write a program or function that takes a six digit hexadecimal colour code and outputs the closest three-digit colour code.
Here's an example:
- Input hex code: #28a086
- Red component
- 0x28 = 40 (decimal)
- 0x22 = 30
- 0x33 = 51
- 0x22 is closer, so the first digit of the shortened colour code is 2
- Green component
- 0xa0 = 160
- 0x99 = 153
- 0xaa = 170
- 0x99 is closer, so the second digit is 9
- Blue component
- 0x86 = 134
- 0x77 = 119
- 0x88 = 136
- 0x88 is closer, so the third digit is 8
- The shortened colour code is #298 (which expands to #229988)
Your program or function must accept as input a six digit hexadecimal colour code prepended with #
and output a three digit colour code prepended with #
.
Examples
- #FF0000 → #F00
- #00FF00 → #0F0
- #D913C4 → #D1C
- #C0DD39 → #BD3
- #28A086 → #298
- #C0CF6F → #BC7
Scoring
This is a code-golf challenge, so shortest answer in your language wins! Standard rules apply.
code-golf hexadecimal color
$endgroup$
$begingroup$
"adding together the difference between each component of the full colour code and the corresponding component of the shorthand colour code" - this part is confusing. There's no adding anywhere, right?
$endgroup$
– Grzegorz Oledzki
7 hours ago
$begingroup$
Note that if you simply drop alternate digits then each short colour represents an equal number of full colours, so that could be considered to make a better representation than nearest colour.
$endgroup$
– Neil
7 hours ago
3
$begingroup$
Saw this in the Sandbox but forgot to mention that I don't think requiring the#
adds anything to the challenge.
$endgroup$
– Shaggy
7 hours ago
$begingroup$
@GrzegorzOledzki you're right, that part is confusing. I'll go ahead an remove it because I think the example in that section is enough to explain what I mean.
$endgroup$
– wrymug
7 hours ago
1
$begingroup$
May we output in lowercase?
$endgroup$
– Arnauld
7 hours ago
|
show 1 more comment
$begingroup$
In CSS, colours can be specified by a "hex triplet" - a three byte (six digit) hexadecimal number where each byte represents the red, green, or blue components of the colour. For instance, #FF0000
is completely red, and is equivalent to rgb(255, 0, 0)
.
Colours can also be represented by the shorthand notation which uses three hexadecimal digits. The shorthand expands to the six digit form by duplicating each digit. For instance, #ABC
becomes #AABBCC
.
Since there are fewer digits in the hex shorthand, fewer colours can be represented.
The challenge
Write a program or function that takes a six digit hexadecimal colour code and outputs the closest three-digit colour code.
Here's an example:
- Input hex code: #28a086
- Red component
- 0x28 = 40 (decimal)
- 0x22 = 30
- 0x33 = 51
- 0x22 is closer, so the first digit of the shortened colour code is 2
- Green component
- 0xa0 = 160
- 0x99 = 153
- 0xaa = 170
- 0x99 is closer, so the second digit is 9
- Blue component
- 0x86 = 134
- 0x77 = 119
- 0x88 = 136
- 0x88 is closer, so the third digit is 8
- The shortened colour code is #298 (which expands to #229988)
Your program or function must accept as input a six digit hexadecimal colour code prepended with #
and output a three digit colour code prepended with #
.
Examples
- #FF0000 → #F00
- #00FF00 → #0F0
- #D913C4 → #D1C
- #C0DD39 → #BD3
- #28A086 → #298
- #C0CF6F → #BC7
Scoring
This is a code-golf challenge, so shortest answer in your language wins! Standard rules apply.
code-golf hexadecimal color
$endgroup$
In CSS, colours can be specified by a "hex triplet" - a three byte (six digit) hexadecimal number where each byte represents the red, green, or blue components of the colour. For instance, #FF0000
is completely red, and is equivalent to rgb(255, 0, 0)
.
Colours can also be represented by the shorthand notation which uses three hexadecimal digits. The shorthand expands to the six digit form by duplicating each digit. For instance, #ABC
becomes #AABBCC
.
Since there are fewer digits in the hex shorthand, fewer colours can be represented.
The challenge
Write a program or function that takes a six digit hexadecimal colour code and outputs the closest three-digit colour code.
Here's an example:
- Input hex code: #28a086
- Red component
- 0x28 = 40 (decimal)
- 0x22 = 30
- 0x33 = 51
- 0x22 is closer, so the first digit of the shortened colour code is 2
- Green component
- 0xa0 = 160
- 0x99 = 153
- 0xaa = 170
- 0x99 is closer, so the second digit is 9
- Blue component
- 0x86 = 134
- 0x77 = 119
- 0x88 = 136
- 0x88 is closer, so the third digit is 8
- The shortened colour code is #298 (which expands to #229988)
Your program or function must accept as input a six digit hexadecimal colour code prepended with #
and output a three digit colour code prepended with #
.
Examples
- #FF0000 → #F00
- #00FF00 → #0F0
- #D913C4 → #D1C
- #C0DD39 → #BD3
- #28A086 → #298
- #C0CF6F → #BC7
Scoring
This is a code-golf challenge, so shortest answer in your language wins! Standard rules apply.
code-golf hexadecimal color
code-golf hexadecimal color
edited 7 hours ago
wrymug
asked 8 hours ago
wrymugwrymug
5572 silver badges13 bronze badges
5572 silver badges13 bronze badges
$begingroup$
"adding together the difference between each component of the full colour code and the corresponding component of the shorthand colour code" - this part is confusing. There's no adding anywhere, right?
$endgroup$
– Grzegorz Oledzki
7 hours ago
$begingroup$
Note that if you simply drop alternate digits then each short colour represents an equal number of full colours, so that could be considered to make a better representation than nearest colour.
$endgroup$
– Neil
7 hours ago
3
$begingroup$
Saw this in the Sandbox but forgot to mention that I don't think requiring the#
adds anything to the challenge.
$endgroup$
– Shaggy
7 hours ago
$begingroup$
@GrzegorzOledzki you're right, that part is confusing. I'll go ahead an remove it because I think the example in that section is enough to explain what I mean.
$endgroup$
– wrymug
7 hours ago
1
$begingroup$
May we output in lowercase?
$endgroup$
– Arnauld
7 hours ago
|
show 1 more comment
$begingroup$
"adding together the difference between each component of the full colour code and the corresponding component of the shorthand colour code" - this part is confusing. There's no adding anywhere, right?
$endgroup$
– Grzegorz Oledzki
7 hours ago
$begingroup$
Note that if you simply drop alternate digits then each short colour represents an equal number of full colours, so that could be considered to make a better representation than nearest colour.
$endgroup$
– Neil
7 hours ago
3
$begingroup$
Saw this in the Sandbox but forgot to mention that I don't think requiring the#
adds anything to the challenge.
$endgroup$
– Shaggy
7 hours ago
$begingroup$
@GrzegorzOledzki you're right, that part is confusing. I'll go ahead an remove it because I think the example in that section is enough to explain what I mean.
$endgroup$
– wrymug
7 hours ago
1
$begingroup$
May we output in lowercase?
$endgroup$
– Arnauld
7 hours ago
$begingroup$
"adding together the difference between each component of the full colour code and the corresponding component of the shorthand colour code" - this part is confusing. There's no adding anywhere, right?
$endgroup$
– Grzegorz Oledzki
7 hours ago
$begingroup$
"adding together the difference between each component of the full colour code and the corresponding component of the shorthand colour code" - this part is confusing. There's no adding anywhere, right?
$endgroup$
– Grzegorz Oledzki
7 hours ago
$begingroup$
Note that if you simply drop alternate digits then each short colour represents an equal number of full colours, so that could be considered to make a better representation than nearest colour.
$endgroup$
– Neil
7 hours ago
$begingroup$
Note that if you simply drop alternate digits then each short colour represents an equal number of full colours, so that could be considered to make a better representation than nearest colour.
$endgroup$
– Neil
7 hours ago
3
3
$begingroup$
Saw this in the Sandbox but forgot to mention that I don't think requiring the
#
adds anything to the challenge.$endgroup$
– Shaggy
7 hours ago
$begingroup$
Saw this in the Sandbox but forgot to mention that I don't think requiring the
#
adds anything to the challenge.$endgroup$
– Shaggy
7 hours ago
$begingroup$
@GrzegorzOledzki you're right, that part is confusing. I'll go ahead an remove it because I think the example in that section is enough to explain what I mean.
$endgroup$
– wrymug
7 hours ago
$begingroup$
@GrzegorzOledzki you're right, that part is confusing. I'll go ahead an remove it because I think the example in that section is enough to explain what I mean.
$endgroup$
– wrymug
7 hours ago
1
1
$begingroup$
May we output in lowercase?
$endgroup$
– Arnauld
7 hours ago
$begingroup$
May we output in lowercase?
$endgroup$
– Arnauld
7 hours ago
|
show 1 more comment
9 Answers
9
active
oldest
votes
$begingroup$
Japt, 16 bytes
r"%w"²_n16_r17Ãg
Try it or run all test cases
r"%w"²_n16_r17Ãg :Implicit input of string
r :Replace
"%w" :RegEx /w/g
² :Duplicate, giving /ww/g
_ :Pass each match through a function
n16 : Convert to decimal
_ : Pass through the following function, and convert back to hex
r17 : Round to the nearest multiple of 17
à : End function
g : Get first character
$endgroup$
add a comment |
$begingroup$
Python 3, 72 70 68 bytes
lambda x:'#'+''.join(f"(int(x[i:i+2],16)+8)//17:X"for i in(1,3,5))
Try it online!
This is a port of Grzegorz Oledzkis original answer, which I helped him golfing down.
Two features of Python 3 help us save bytes:
- Floating point division by default
- Format string literals
-2 bytes thanx to Jonathan Allan
$endgroup$
1
$begingroup$
(int(x[i:i+2],16)+8)//17
saves 2
$endgroup$
– Jonathan Allan
4 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 55 bytes
s=>s.replace(/w./g,x=>(('0x'+x)/17+.5|0).toString(16))
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 2 (109 101 97 85 83 74 bytes)
lambda x:'#'+''.join(hex(int(int(x[i:i+2],16)/17.+.5))[2:]for i in[1,3,5])
The "nearest distance" is handled by division by 17 and rounding.
Improvements:
-8 bytes by using the int(...+.5)
trick instead of int(round(...))
-4 bytes by using list comprehension instead of map()
-1 byte by hardcoding #
in the output (thanks @movatica)
-10 bytes by not using re.findall("..",...)
in favor of explicit String splicing
-2 bytes by not using list comprehension, but an inline generator expression in join
(thanks @movatica)
-1 byte by not splicing the :7
ending for blue part
-9 bytes by better iteration over colors - i.e. iterating over indices, not actual characters (thanks @movatica)
$endgroup$
$begingroup$
Does not run withoutimport re
. The import is required and thus adds to the bytecount!
$endgroup$
– movatica
6 hours ago
1
$begingroup$
@movatica - you're right, added it
$endgroup$
– Grzegorz Oledzki
6 hours ago
1
$begingroup$
Save 1 byte by hardcoding'#'
instead ofx[0]
.
$endgroup$
– movatica
6 hours ago
1
$begingroup$
You can skip the list comprehension inside''.join(...)
, as it also handles a generator expression. Just remove the[]
and save 2 more bytes :)
$endgroup$
– movatica
6 hours ago
1
$begingroup$
Thanks!range(1,6,2)
is even better with[1,3,5]
$endgroup$
– Grzegorz Oledzki
6 hours ago
|
show 4 more comments
$begingroup$
PHP, 75 67 bytes
#<?php for($m=3;$m;)echo dechex((hexdec($argn)>>--$m*8&255)/17+.5);
Try it online! or verify all test cases.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 88 bytes
(w)(.)
$1,$2;
[A-F]
1$&
T`L`d
d+
$*
+`1,
,16$*
,
8$*
(117)*1*;
$#1;
T`d`L`1d
BB|;
Try it online! Link includes test cases. Explanation:
(w)(.)
$1,$2;
Pair up the hex digits.
[A-F]
1$&
T`L`d
Convert each digit separately to decimal.
d+
$*
Convert each decimal digit to unary.
+`1,
,16$*
Finish the hexadecimal conversion of the pair of digits.
,
8$*
(117)*1*;
$#1;
Add 8 and divide by 17.
T`d`L`1d
BB|;
Convert back to hexadecimal.
$endgroup$
add a comment |
$begingroup$
Jelly, 20 bytes
ḊØHiⱮs2ḅ⁴÷17+.ḞịØHṭḢ
Try it online!
$endgroup$
add a comment |
$begingroup$
05AB1E, 13 bytes
ćs2ôH8+17÷hJ«
Try it online!
How?
ćs2ôH8+17÷hJ« | string, S e.g. stack: "#B23F08"
ć | decapitate "B23F08", "#"
s | swap "#", "B23F08"
2 | two "#", "B23F08", 2
ô | chuncks "#", ["B2", "3F", "08"]
H | from hexadecimal "#", [178, 63, 8]
8 | eight "#", [178, 63, 8], 8
+ | add "#", [186, 71, 16]
17 | seventeen "#", [186, 71, 16], 17
÷ | integer divide "#", [10, 4, 0]
h | to hexadecimal "#", ["A", "4", "0"]
J | join "#", "A40"
« | concatenate "#A40"
| print top of stack
$endgroup$
add a comment |
$begingroup$
Perl 5 -p
, 35 bytes
s|ww|sprintf'%X',.5+(hex$&)/17|ge
Try it online!
Reads from STDIN, replaces each pair of items that is not #
with the appropriate single character using the division by 17 method for finding the nearest, then implicitly outputs (-p
) the result.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f187465%2ffind-the-closest-three-digit-hex-colour%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Japt, 16 bytes
r"%w"²_n16_r17Ãg
Try it or run all test cases
r"%w"²_n16_r17Ãg :Implicit input of string
r :Replace
"%w" :RegEx /w/g
² :Duplicate, giving /ww/g
_ :Pass each match through a function
n16 : Convert to decimal
_ : Pass through the following function, and convert back to hex
r17 : Round to the nearest multiple of 17
à : End function
g : Get first character
$endgroup$
add a comment |
$begingroup$
Japt, 16 bytes
r"%w"²_n16_r17Ãg
Try it or run all test cases
r"%w"²_n16_r17Ãg :Implicit input of string
r :Replace
"%w" :RegEx /w/g
² :Duplicate, giving /ww/g
_ :Pass each match through a function
n16 : Convert to decimal
_ : Pass through the following function, and convert back to hex
r17 : Round to the nearest multiple of 17
à : End function
g : Get first character
$endgroup$
add a comment |
$begingroup$
Japt, 16 bytes
r"%w"²_n16_r17Ãg
Try it or run all test cases
r"%w"²_n16_r17Ãg :Implicit input of string
r :Replace
"%w" :RegEx /w/g
² :Duplicate, giving /ww/g
_ :Pass each match through a function
n16 : Convert to decimal
_ : Pass through the following function, and convert back to hex
r17 : Round to the nearest multiple of 17
à : End function
g : Get first character
$endgroup$
Japt, 16 bytes
r"%w"²_n16_r17Ãg
Try it or run all test cases
r"%w"²_n16_r17Ãg :Implicit input of string
r :Replace
"%w" :RegEx /w/g
² :Duplicate, giving /ww/g
_ :Pass each match through a function
n16 : Convert to decimal
_ : Pass through the following function, and convert back to hex
r17 : Round to the nearest multiple of 17
à : End function
g : Get first character
edited 6 hours ago
answered 7 hours ago
ShaggyShaggy
20k3 gold badges20 silver badges68 bronze badges
20k3 gold badges20 silver badges68 bronze badges
add a comment |
add a comment |
$begingroup$
Python 3, 72 70 68 bytes
lambda x:'#'+''.join(f"(int(x[i:i+2],16)+8)//17:X"for i in(1,3,5))
Try it online!
This is a port of Grzegorz Oledzkis original answer, which I helped him golfing down.
Two features of Python 3 help us save bytes:
- Floating point division by default
- Format string literals
-2 bytes thanx to Jonathan Allan
$endgroup$
1
$begingroup$
(int(x[i:i+2],16)+8)//17
saves 2
$endgroup$
– Jonathan Allan
4 hours ago
add a comment |
$begingroup$
Python 3, 72 70 68 bytes
lambda x:'#'+''.join(f"(int(x[i:i+2],16)+8)//17:X"for i in(1,3,5))
Try it online!
This is a port of Grzegorz Oledzkis original answer, which I helped him golfing down.
Two features of Python 3 help us save bytes:
- Floating point division by default
- Format string literals
-2 bytes thanx to Jonathan Allan
$endgroup$
1
$begingroup$
(int(x[i:i+2],16)+8)//17
saves 2
$endgroup$
– Jonathan Allan
4 hours ago
add a comment |
$begingroup$
Python 3, 72 70 68 bytes
lambda x:'#'+''.join(f"(int(x[i:i+2],16)+8)//17:X"for i in(1,3,5))
Try it online!
This is a port of Grzegorz Oledzkis original answer, which I helped him golfing down.
Two features of Python 3 help us save bytes:
- Floating point division by default
- Format string literals
-2 bytes thanx to Jonathan Allan
$endgroup$
Python 3, 72 70 68 bytes
lambda x:'#'+''.join(f"(int(x[i:i+2],16)+8)//17:X"for i in(1,3,5))
Try it online!
This is a port of Grzegorz Oledzkis original answer, which I helped him golfing down.
Two features of Python 3 help us save bytes:
- Floating point division by default
- Format string literals
-2 bytes thanx to Jonathan Allan
edited 4 hours ago
answered 6 hours ago
movaticamovatica
3286 bronze badges
3286 bronze badges
1
$begingroup$
(int(x[i:i+2],16)+8)//17
saves 2
$endgroup$
– Jonathan Allan
4 hours ago
add a comment |
1
$begingroup$
(int(x[i:i+2],16)+8)//17
saves 2
$endgroup$
– Jonathan Allan
4 hours ago
1
1
$begingroup$
(int(x[i:i+2],16)+8)//17
saves 2$endgroup$
– Jonathan Allan
4 hours ago
$begingroup$
(int(x[i:i+2],16)+8)//17
saves 2$endgroup$
– Jonathan Allan
4 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 55 bytes
s=>s.replace(/w./g,x=>(('0x'+x)/17+.5|0).toString(16))
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 55 bytes
s=>s.replace(/w./g,x=>(('0x'+x)/17+.5|0).toString(16))
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 55 bytes
s=>s.replace(/w./g,x=>(('0x'+x)/17+.5|0).toString(16))
Try it online!
$endgroup$
JavaScript (ES6), 55 bytes
s=>s.replace(/w./g,x=>(('0x'+x)/17+.5|0).toString(16))
Try it online!
answered 6 hours ago
ArnauldArnauld
87.1k7 gold badges102 silver badges356 bronze badges
87.1k7 gold badges102 silver badges356 bronze badges
add a comment |
add a comment |
$begingroup$
Python 2 (109 101 97 85 83 74 bytes)
lambda x:'#'+''.join(hex(int(int(x[i:i+2],16)/17.+.5))[2:]for i in[1,3,5])
The "nearest distance" is handled by division by 17 and rounding.
Improvements:
-8 bytes by using the int(...+.5)
trick instead of int(round(...))
-4 bytes by using list comprehension instead of map()
-1 byte by hardcoding #
in the output (thanks @movatica)
-10 bytes by not using re.findall("..",...)
in favor of explicit String splicing
-2 bytes by not using list comprehension, but an inline generator expression in join
(thanks @movatica)
-1 byte by not splicing the :7
ending for blue part
-9 bytes by better iteration over colors - i.e. iterating over indices, not actual characters (thanks @movatica)
$endgroup$
$begingroup$
Does not run withoutimport re
. The import is required and thus adds to the bytecount!
$endgroup$
– movatica
6 hours ago
1
$begingroup$
@movatica - you're right, added it
$endgroup$
– Grzegorz Oledzki
6 hours ago
1
$begingroup$
Save 1 byte by hardcoding'#'
instead ofx[0]
.
$endgroup$
– movatica
6 hours ago
1
$begingroup$
You can skip the list comprehension inside''.join(...)
, as it also handles a generator expression. Just remove the[]
and save 2 more bytes :)
$endgroup$
– movatica
6 hours ago
1
$begingroup$
Thanks!range(1,6,2)
is even better with[1,3,5]
$endgroup$
– Grzegorz Oledzki
6 hours ago
|
show 4 more comments
$begingroup$
Python 2 (109 101 97 85 83 74 bytes)
lambda x:'#'+''.join(hex(int(int(x[i:i+2],16)/17.+.5))[2:]for i in[1,3,5])
The "nearest distance" is handled by division by 17 and rounding.
Improvements:
-8 bytes by using the int(...+.5)
trick instead of int(round(...))
-4 bytes by using list comprehension instead of map()
-1 byte by hardcoding #
in the output (thanks @movatica)
-10 bytes by not using re.findall("..",...)
in favor of explicit String splicing
-2 bytes by not using list comprehension, but an inline generator expression in join
(thanks @movatica)
-1 byte by not splicing the :7
ending for blue part
-9 bytes by better iteration over colors - i.e. iterating over indices, not actual characters (thanks @movatica)
$endgroup$
$begingroup$
Does not run withoutimport re
. The import is required and thus adds to the bytecount!
$endgroup$
– movatica
6 hours ago
1
$begingroup$
@movatica - you're right, added it
$endgroup$
– Grzegorz Oledzki
6 hours ago
1
$begingroup$
Save 1 byte by hardcoding'#'
instead ofx[0]
.
$endgroup$
– movatica
6 hours ago
1
$begingroup$
You can skip the list comprehension inside''.join(...)
, as it also handles a generator expression. Just remove the[]
and save 2 more bytes :)
$endgroup$
– movatica
6 hours ago
1
$begingroup$
Thanks!range(1,6,2)
is even better with[1,3,5]
$endgroup$
– Grzegorz Oledzki
6 hours ago
|
show 4 more comments
$begingroup$
Python 2 (109 101 97 85 83 74 bytes)
lambda x:'#'+''.join(hex(int(int(x[i:i+2],16)/17.+.5))[2:]for i in[1,3,5])
The "nearest distance" is handled by division by 17 and rounding.
Improvements:
-8 bytes by using the int(...+.5)
trick instead of int(round(...))
-4 bytes by using list comprehension instead of map()
-1 byte by hardcoding #
in the output (thanks @movatica)
-10 bytes by not using re.findall("..",...)
in favor of explicit String splicing
-2 bytes by not using list comprehension, but an inline generator expression in join
(thanks @movatica)
-1 byte by not splicing the :7
ending for blue part
-9 bytes by better iteration over colors - i.e. iterating over indices, not actual characters (thanks @movatica)
$endgroup$
Python 2 (109 101 97 85 83 74 bytes)
lambda x:'#'+''.join(hex(int(int(x[i:i+2],16)/17.+.5))[2:]for i in[1,3,5])
The "nearest distance" is handled by division by 17 and rounding.
Improvements:
-8 bytes by using the int(...+.5)
trick instead of int(round(...))
-4 bytes by using list comprehension instead of map()
-1 byte by hardcoding #
in the output (thanks @movatica)
-10 bytes by not using re.findall("..",...)
in favor of explicit String splicing
-2 bytes by not using list comprehension, but an inline generator expression in join
(thanks @movatica)
-1 byte by not splicing the :7
ending for blue part
-9 bytes by better iteration over colors - i.e. iterating over indices, not actual characters (thanks @movatica)
edited 6 hours ago
answered 7 hours ago
Grzegorz OledzkiGrzegorz Oledzki
1737 bronze badges
1737 bronze badges
$begingroup$
Does not run withoutimport re
. The import is required and thus adds to the bytecount!
$endgroup$
– movatica
6 hours ago
1
$begingroup$
@movatica - you're right, added it
$endgroup$
– Grzegorz Oledzki
6 hours ago
1
$begingroup$
Save 1 byte by hardcoding'#'
instead ofx[0]
.
$endgroup$
– movatica
6 hours ago
1
$begingroup$
You can skip the list comprehension inside''.join(...)
, as it also handles a generator expression. Just remove the[]
and save 2 more bytes :)
$endgroup$
– movatica
6 hours ago
1
$begingroup$
Thanks!range(1,6,2)
is even better with[1,3,5]
$endgroup$
– Grzegorz Oledzki
6 hours ago
|
show 4 more comments
$begingroup$
Does not run withoutimport re
. The import is required and thus adds to the bytecount!
$endgroup$
– movatica
6 hours ago
1
$begingroup$
@movatica - you're right, added it
$endgroup$
– Grzegorz Oledzki
6 hours ago
1
$begingroup$
Save 1 byte by hardcoding'#'
instead ofx[0]
.
$endgroup$
– movatica
6 hours ago
1
$begingroup$
You can skip the list comprehension inside''.join(...)
, as it also handles a generator expression. Just remove the[]
and save 2 more bytes :)
$endgroup$
– movatica
6 hours ago
1
$begingroup$
Thanks!range(1,6,2)
is even better with[1,3,5]
$endgroup$
– Grzegorz Oledzki
6 hours ago
$begingroup$
Does not run without
import re
. The import is required and thus adds to the bytecount!$endgroup$
– movatica
6 hours ago
$begingroup$
Does not run without
import re
. The import is required and thus adds to the bytecount!$endgroup$
– movatica
6 hours ago
1
1
$begingroup$
@movatica - you're right, added it
$endgroup$
– Grzegorz Oledzki
6 hours ago
$begingroup$
@movatica - you're right, added it
$endgroup$
– Grzegorz Oledzki
6 hours ago
1
1
$begingroup$
Save 1 byte by hardcoding
'#'
instead of x[0]
.$endgroup$
– movatica
6 hours ago
$begingroup$
Save 1 byte by hardcoding
'#'
instead of x[0]
.$endgroup$
– movatica
6 hours ago
1
1
$begingroup$
You can skip the list comprehension inside
''.join(...)
, as it also handles a generator expression. Just remove the []
and save 2 more bytes :)$endgroup$
– movatica
6 hours ago
$begingroup$
You can skip the list comprehension inside
''.join(...)
, as it also handles a generator expression. Just remove the []
and save 2 more bytes :)$endgroup$
– movatica
6 hours ago
1
1
$begingroup$
Thanks!
range(1,6,2)
is even better with [1,3,5]
$endgroup$
– Grzegorz Oledzki
6 hours ago
$begingroup$
Thanks!
range(1,6,2)
is even better with [1,3,5]
$endgroup$
– Grzegorz Oledzki
6 hours ago
|
show 4 more comments
$begingroup$
PHP, 75 67 bytes
#<?php for($m=3;$m;)echo dechex((hexdec($argn)>>--$m*8&255)/17+.5);
Try it online! or verify all test cases.
$endgroup$
add a comment |
$begingroup$
PHP, 75 67 bytes
#<?php for($m=3;$m;)echo dechex((hexdec($argn)>>--$m*8&255)/17+.5);
Try it online! or verify all test cases.
$endgroup$
add a comment |
$begingroup$
PHP, 75 67 bytes
#<?php for($m=3;$m;)echo dechex((hexdec($argn)>>--$m*8&255)/17+.5);
Try it online! or verify all test cases.
$endgroup$
PHP, 75 67 bytes
#<?php for($m=3;$m;)echo dechex((hexdec($argn)>>--$m*8&255)/17+.5);
Try it online! or verify all test cases.
edited 5 hours ago
answered 6 hours ago
gwaughgwaugh
3,2861 gold badge8 silver badges23 bronze badges
3,2861 gold badge8 silver badges23 bronze badges
add a comment |
add a comment |
$begingroup$
Retina 0.8.2, 88 bytes
(w)(.)
$1,$2;
[A-F]
1$&
T`L`d
d+
$*
+`1,
,16$*
,
8$*
(117)*1*;
$#1;
T`d`L`1d
BB|;
Try it online! Link includes test cases. Explanation:
(w)(.)
$1,$2;
Pair up the hex digits.
[A-F]
1$&
T`L`d
Convert each digit separately to decimal.
d+
$*
Convert each decimal digit to unary.
+`1,
,16$*
Finish the hexadecimal conversion of the pair of digits.
,
8$*
(117)*1*;
$#1;
Add 8 and divide by 17.
T`d`L`1d
BB|;
Convert back to hexadecimal.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 88 bytes
(w)(.)
$1,$2;
[A-F]
1$&
T`L`d
d+
$*
+`1,
,16$*
,
8$*
(117)*1*;
$#1;
T`d`L`1d
BB|;
Try it online! Link includes test cases. Explanation:
(w)(.)
$1,$2;
Pair up the hex digits.
[A-F]
1$&
T`L`d
Convert each digit separately to decimal.
d+
$*
Convert each decimal digit to unary.
+`1,
,16$*
Finish the hexadecimal conversion of the pair of digits.
,
8$*
(117)*1*;
$#1;
Add 8 and divide by 17.
T`d`L`1d
BB|;
Convert back to hexadecimal.
$endgroup$
add a comment |
$begingroup$
Retina 0.8.2, 88 bytes
(w)(.)
$1,$2;
[A-F]
1$&
T`L`d
d+
$*
+`1,
,16$*
,
8$*
(117)*1*;
$#1;
T`d`L`1d
BB|;
Try it online! Link includes test cases. Explanation:
(w)(.)
$1,$2;
Pair up the hex digits.
[A-F]
1$&
T`L`d
Convert each digit separately to decimal.
d+
$*
Convert each decimal digit to unary.
+`1,
,16$*
Finish the hexadecimal conversion of the pair of digits.
,
8$*
(117)*1*;
$#1;
Add 8 and divide by 17.
T`d`L`1d
BB|;
Convert back to hexadecimal.
$endgroup$
Retina 0.8.2, 88 bytes
(w)(.)
$1,$2;
[A-F]
1$&
T`L`d
d+
$*
+`1,
,16$*
,
8$*
(117)*1*;
$#1;
T`d`L`1d
BB|;
Try it online! Link includes test cases. Explanation:
(w)(.)
$1,$2;
Pair up the hex digits.
[A-F]
1$&
T`L`d
Convert each digit separately to decimal.
d+
$*
Convert each decimal digit to unary.
+`1,
,16$*
Finish the hexadecimal conversion of the pair of digits.
,
8$*
(117)*1*;
$#1;
Add 8 and divide by 17.
T`d`L`1d
BB|;
Convert back to hexadecimal.
answered 7 hours ago
NeilNeil
85.7k8 gold badges46 silver badges183 bronze badges
85.7k8 gold badges46 silver badges183 bronze badges
add a comment |
add a comment |
$begingroup$
Jelly, 20 bytes
ḊØHiⱮs2ḅ⁴÷17+.ḞịØHṭḢ
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 20 bytes
ḊØHiⱮs2ḅ⁴÷17+.ḞịØHṭḢ
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 20 bytes
ḊØHiⱮs2ḅ⁴÷17+.ḞịØHṭḢ
Try it online!
$endgroup$
Jelly, 20 bytes
ḊØHiⱮs2ḅ⁴÷17+.ḞịØHṭḢ
Try it online!
answered 4 hours ago
Nick KennedyNick Kennedy
3,8847 silver badges12 bronze badges
3,8847 silver badges12 bronze badges
add a comment |
add a comment |
$begingroup$
05AB1E, 13 bytes
ćs2ôH8+17÷hJ«
Try it online!
How?
ćs2ôH8+17÷hJ« | string, S e.g. stack: "#B23F08"
ć | decapitate "B23F08", "#"
s | swap "#", "B23F08"
2 | two "#", "B23F08", 2
ô | chuncks "#", ["B2", "3F", "08"]
H | from hexadecimal "#", [178, 63, 8]
8 | eight "#", [178, 63, 8], 8
+ | add "#", [186, 71, 16]
17 | seventeen "#", [186, 71, 16], 17
÷ | integer divide "#", [10, 4, 0]
h | to hexadecimal "#", ["A", "4", "0"]
J | join "#", "A40"
« | concatenate "#A40"
| print top of stack
$endgroup$
add a comment |
$begingroup$
05AB1E, 13 bytes
ćs2ôH8+17÷hJ«
Try it online!
How?
ćs2ôH8+17÷hJ« | string, S e.g. stack: "#B23F08"
ć | decapitate "B23F08", "#"
s | swap "#", "B23F08"
2 | two "#", "B23F08", 2
ô | chuncks "#", ["B2", "3F", "08"]
H | from hexadecimal "#", [178, 63, 8]
8 | eight "#", [178, 63, 8], 8
+ | add "#", [186, 71, 16]
17 | seventeen "#", [186, 71, 16], 17
÷ | integer divide "#", [10, 4, 0]
h | to hexadecimal "#", ["A", "4", "0"]
J | join "#", "A40"
« | concatenate "#A40"
| print top of stack
$endgroup$
add a comment |
$begingroup$
05AB1E, 13 bytes
ćs2ôH8+17÷hJ«
Try it online!
How?
ćs2ôH8+17÷hJ« | string, S e.g. stack: "#B23F08"
ć | decapitate "B23F08", "#"
s | swap "#", "B23F08"
2 | two "#", "B23F08", 2
ô | chuncks "#", ["B2", "3F", "08"]
H | from hexadecimal "#", [178, 63, 8]
8 | eight "#", [178, 63, 8], 8
+ | add "#", [186, 71, 16]
17 | seventeen "#", [186, 71, 16], 17
÷ | integer divide "#", [10, 4, 0]
h | to hexadecimal "#", ["A", "4", "0"]
J | join "#", "A40"
« | concatenate "#A40"
| print top of stack
$endgroup$
05AB1E, 13 bytes
ćs2ôH8+17÷hJ«
Try it online!
How?
ćs2ôH8+17÷hJ« | string, S e.g. stack: "#B23F08"
ć | decapitate "B23F08", "#"
s | swap "#", "B23F08"
2 | two "#", "B23F08", 2
ô | chuncks "#", ["B2", "3F", "08"]
H | from hexadecimal "#", [178, 63, 8]
8 | eight "#", [178, 63, 8], 8
+ | add "#", [186, 71, 16]
17 | seventeen "#", [186, 71, 16], 17
÷ | integer divide "#", [10, 4, 0]
h | to hexadecimal "#", ["A", "4", "0"]
J | join "#", "A40"
« | concatenate "#A40"
| print top of stack
edited 3 hours ago
answered 4 hours ago
Jonathan AllanJonathan Allan
56.5k5 gold badges41 silver badges178 bronze badges
56.5k5 gold badges41 silver badges178 bronze badges
add a comment |
add a comment |
$begingroup$
Perl 5 -p
, 35 bytes
s|ww|sprintf'%X',.5+(hex$&)/17|ge
Try it online!
Reads from STDIN, replaces each pair of items that is not #
with the appropriate single character using the division by 17 method for finding the nearest, then implicitly outputs (-p
) the result.
$endgroup$
add a comment |
$begingroup$
Perl 5 -p
, 35 bytes
s|ww|sprintf'%X',.5+(hex$&)/17|ge
Try it online!
Reads from STDIN, replaces each pair of items that is not #
with the appropriate single character using the division by 17 method for finding the nearest, then implicitly outputs (-p
) the result.
$endgroup$
add a comment |
$begingroup$
Perl 5 -p
, 35 bytes
s|ww|sprintf'%X',.5+(hex$&)/17|ge
Try it online!
Reads from STDIN, replaces each pair of items that is not #
with the appropriate single character using the division by 17 method for finding the nearest, then implicitly outputs (-p
) the result.
$endgroup$
Perl 5 -p
, 35 bytes
s|ww|sprintf'%X',.5+(hex$&)/17|ge
Try it online!
Reads from STDIN, replaces each pair of items that is not #
with the appropriate single character using the division by 17 method for finding the nearest, then implicitly outputs (-p
) the result.
answered 1 hour ago
XcaliXcali
6,1975 silver badges23 bronze badges
6,1975 silver badges23 bronze badges
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f187465%2ffind-the-closest-three-digit-hex-colour%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
"adding together the difference between each component of the full colour code and the corresponding component of the shorthand colour code" - this part is confusing. There's no adding anywhere, right?
$endgroup$
– Grzegorz Oledzki
7 hours ago
$begingroup$
Note that if you simply drop alternate digits then each short colour represents an equal number of full colours, so that could be considered to make a better representation than nearest colour.
$endgroup$
– Neil
7 hours ago
3
$begingroup$
Saw this in the Sandbox but forgot to mention that I don't think requiring the
#
adds anything to the challenge.$endgroup$
– Shaggy
7 hours ago
$begingroup$
@GrzegorzOledzki you're right, that part is confusing. I'll go ahead an remove it because I think the example in that section is enough to explain what I mean.
$endgroup$
– wrymug
7 hours ago
1
$begingroup$
May we output in lowercase?
$endgroup$
– Arnauld
7 hours ago