q/kdb+, 55 bytes
Solution:
"#",raze{(last($)0x0 vs)each"h"$.0255*x[3]*x 0 1 2}100-
Examples:
q)"#",raze{(last($)0x0 vs)each"h"$.0255*x[3]*x 0 1 2}100-86 86 0 43
"#141491"
q)"#",raze{(last($)0x0 vs)each"h"$.0255*x[3]*x 0 1 2}100-28 14 0 6
"#adcef0"
q)"#",raze{(last($)0x0 vs)each"h"$.0255*x[3]*x 0 1 2}100-0 41 73 4
"#f59042"
Explanation:
Fairly straightforward, stole the 0.0255
trick from other solutions (thanks!). Evaluation is performed right to left.
"#",raze {(last string 0x0 vs) each "h"$ .0255 * a[3] * a 0 1 2}100- / ungolfed
{ } / lambda function
100- / subtract from 100 (vector)
a 0 1 2 / index into a at 0, 1 and 2 (CMY)
a[3] / index into at at 3 (K)
* / multiply together
.0255 * / multiply by 0.255
"h"$ / cast to shorts
( ) each / perform stuff in brackets on each list item
0x0 vs / converts to hex, 1 -> 0x0001
string / cast to string, 0x0001 -> ["00", "01"]
last / take the last one, "01"
raze / join strings together
"#", / prepend the hash
Notes:
Rounds numbers by default, would cost 3 bytes (_)
to floor instead before casting to short.
1Can we take
CMYK
values as decimals from0
to1
or is it required to do0
to100
? – HyperNeutrino – 2017-06-30T17:39:26.5801Also, are we supposed to input multiple CMYK codes at once or just one and convert it? – HyperNeutrino – 2017-06-30T17:41:09.540
7Can we take the input as a list of numbers or does it have to be a delimited string? – Business Cat – 2017-06-30T17:41:26.987
Is the output required to have a
#
at the beginning? – Mr. Xcoder – 2017-06-30T17:58:50.837@Mr.Xcoder Yes it is required. – Wasi – 2017-06-30T18:03:33.663
@BusinessCat String – Wasi – 2017-06-30T18:04:55.723
@HyperNeutrino It is okay if your code just works for one input and converts it. But, you can also submit code that handles multiple CMYK codes as well. However, try to reduce source code size as much as you can. – Wasi – 2017-06-30T18:10:07.913
@HyperNeutrino 0-100. – Wasi – 2017-06-30T18:12:12.200
7The input / output that you provided doesn't match the formula, also how should we handle the rounding? – Rod – 2017-06-30T18:33:13.720
Is uppercase required for
#RRGGBB
format? – LarsW – 2017-06-30T23:31:17.2102@Rod It's a bit unclear yet how floating-point inaccuracies should be handled. – Erik the Outgolfer – 2017-07-01T07:33:36.770