23
\$\begingroup\$

You would simply take this image and make every color added one to every hexadecimal digit. For example, #49de5f would become #5aef60 (with the 9 looping to an a, and the f looping to a 0.)

Color #49de5fColor #5aef60

This would also mean that all white (#ffffff) would become black (#000000) because all f loops back to 0, but all black will become a lighter shade of black (#111111).

Color #000000Color #111111

Scoring is based on the least number of bytes used, as this is a question.

Use the below image as the input image for your code, and put the output image of your code into your answer.

Input image

If you want to, you can also use this other rainbow image:

Another optional input image

\$\endgroup\$
  • \$\begingroup\$ What assumptions can we make on input/output format, if we would like to use a programming language designed for hardware/FPGA design? \$\endgroup\$ – hexafraction Jun 19 '16 at 15:52
  • \$\begingroup\$ @hexafraction I think by default you can take as a file or as hex code iirc. \$\endgroup\$ – Rɪᴋᴇʀ Jun 19 '16 at 16:03
  • \$\begingroup\$ @hexafraction meta.codegolf.stackexchange.com/q/9093/8478 \$\endgroup\$ – Martin Ender Jun 19 '16 at 16:03
  • 5
    \$\begingroup\$ @Peanut, it would be good to have a test case which includes ef bytes (which should become f0 as opposed to 00 which happens when you just add 17 and take mod 256). \$\endgroup\$ – Martin Ender Jun 19 '16 at 17:44
  • 2
    \$\begingroup\$ You should post an image for sample output, rather than just providing sample input. Other than that, this is a very nice first post! Welcome to the site! \$\endgroup\$ – DJMcMayhem Jun 19 '16 at 18:59
5
\$\begingroup\$

Pyke, 17 13 bytes

.Fh16j%ijcjs 8{

Try it here!

.F            - for i in deep_for(input):
  h16%        -    (i+1)%16
          +   -   ^+V
      i16+    -    (i+16)
           8{ -  unset_bit(8, ^)

Takes input as a 3d integer array of pixels and outputs in the same format

RAINBOWZ (No unicorn :()

\$\endgroup\$
  • \$\begingroup\$ Can you provide the output image? \$\endgroup\$ – haykam Jun 26 '16 at 16:03
7
\$\begingroup\$

Mathematica, 78 bytes

Image@Apply[16#+#2&,Mod[IntegerDigits[#~ImageData~"Byte",16,2]+1,16]/255,{3}]&

Takes and returns an image object (to create an image object, just paste the image into Mathematica).

Result for the test case:

enter image description here

Taking input and returning output as a 3D array of integer channel values, this reduces to 51 bytes:

Apply[16#+#2&,Mod[IntegerDigits[#,16,2]+1,16],{3}]&

But those meta posts don't have an overwhelming amount of support yet, so I'm going with the 78-byte version for now.

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

Verilog, 220 bytes:

  • Programs may take input as an array of RGB pixel values with dimensions
  • Programs may output via an array of RGB pixel values with dimensions

It's currently not clear as to how the dimensions are to be provided and if the array is to be streamed or provided all at once. I'm going to stream it 8 bits at a time using a clock signal (with a valid-data flag that goes low after the entire image has been processed) and input/output the dimensions as 32-bit integers:

module a(input[31:0]w,input[31:0]h,input[7:0]d,input c,output[31:0]W,output[31:0]H,output reg[7:0]D,output reg v=0);assign W=w;assign H=h;reg[65:0]p=1;always@(posedge c) begin v<=(p<3*w*h); p<=p+v; D<=d+17; end endmodule
\$\endgroup\$
4
\$\begingroup\$

Python, 226 bytes

Now, it's valid !

Use the Pillow library.

from PIL import Image
m=Image.open(input()).convert("RGB")
for y in range(m.size[1]):
 for x in range(m.size[0]):
    t=m.getpixel((x,y))
    h=t[0]+(t[1]<<8)+(t[2]<<16)+1118481
    m.putpixel((x,y),(h&255,h>>8&255,h>>16&255))
m.show()

Output:Output

Thanks to @TuukkaX for saving 9 bytes !
Thanks to @mbomb007 for saving 18 bytes !

\$\endgroup\$
  • \$\begingroup\$ Is it necessary to use 0xFF instead of 255? \$\endgroup\$ – Yytsi Jun 20 '16 at 23:23
  • \$\begingroup\$ @TuukkaX Woops i haven't noticed that thanks you \$\endgroup\$ – TuxCrafting Jun 21 '16 at 7:10
  • \$\begingroup\$ There is one more 0xFF in there :D \$\endgroup\$ – Yytsi Jun 21 '16 at 7:12
  • \$\begingroup\$ I think you can save more bytes by saying from PIL import*. I also think that Image.open can be changed to just open after that. \$\endgroup\$ – Yytsi Jun 21 '16 at 17:00
  • \$\begingroup\$ @TuukkaX Yes, it can be changed to from PIL import*, but i can't change the Image.open \$\endgroup\$ – TuxCrafting Jun 21 '16 at 17:23
1
\$\begingroup\$

Dyalog APL, 21 15 bytes

Programs may output as a matrix of RGB pixel values

I assume output may be in the same format.

New solution takes matrix of values [[r,g,b,r,g,b],[r,g,b,…

16⊥16|1+16 16⊤⎕

Explanation

get numeric input
16 16⊤ convert to 2-digit base 16
1+ add 1, i.e. 0 → 1, 1 → 2, 15 → 16
16| modulus 16, i.e. 16 → 0
16⊥ convert from base 16

Example

      ⊢m←2 6⍴90 239 96 255 255 255 0 0 0 239 239 239
90 239 96 255 255 255
 0   0  0 239 239 239
      16⊥16|1+⎕⊤⍨2/16
⎕:
      m
107 240 113   0   0   0
 17  17  17 240 240 240

Old 21 byte solution takes matrix of [["RRGGBB","RRGGBB"],["RRGGBB",…

{n[16|1+⍵⍳⍨n←⎕D,⎕A]}¨

Needs ⎕IO←0, which is default on many systems.

Explanation

{ for each RGB 6-char string, represent it as and do:
n←⎕D,⎕A assign "0…9A…Z" to n
⍵⍳⍨ find indices of the individual characters in n
1+ add one to the index, i.e. 0 → 1, 1 → 2, 15 → 16
16| modulus 16, i.e. 16 → 0
n[] use that to index into n

Example

      f←{n[16|1+⍵⍳⍨n←⎕D,⎕A]}¨ 
      ⊢p←2 2⍴'5AEF60' 'FFFFFF' '000000' 'EFEFEF'
┌──────┬──────┐
│5AEF60│FFFFFF│
├──────┼──────┤
│000000│EFEFEF│
└──────┴──────┘
      f p           
┌──────┬──────┐
│6BF071│000000│
├──────┼──────┤
│111111│F0F0F0│
└──────┴──────┘
\$\endgroup\$
1
\$\begingroup\$

C - 114 113 70 66 61 72 67 bytes

Here's the code (with support for Martin Ender's test case (without it's 60b)):

main(c,b){for(;~(b=getchar());putchar(c++<54?b:b+16&240|b+1&15));}

And here is less obfuscated version:

main( c, b ) //Defaults to int
{
    //Get characters until EOF occurs
    //Copy first 54 bytes of header, later add 1 to each hexadecimal digit
    for ( ; ~( b = getchar( ) ); putchar( c++ < 54 ? b: b + 16 & 240 | b + 1 & 15 ) ); 
}

Compile and run with gcc -o color colorgolf.c && cat a.bmp | ./color > b.bmp

This code supports works with bitmaps. To convert png files to bmp, I used following command: convert -flatten -alpha off png.png a.bmp

Code assumes, that bmp header is 54 bytes long - in this case it works, but I'm not sure if I'm not discreetly breaking something.

Also, this is the rainbow:
It looks sad now... :(

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

Java 142 bytes

public BufferedImage translateColor(BufferedImage image){
  for(int i=-1;++i<image.getWidth();)
    for(int j=-1;++<image.getHeight();)
      image.setRGB(i,j,image.getRGB(i,j)+1118481);
  return image;
}

Golfed:

BufferedImage t(BufferedImage i){for(int x=-1;++x<i.getWidth();)for(int y=-1;++y<i.getHeight();)i.setRGB(x,y,i.getRGB(x,y)+1118481);return i;}
\$\endgroup\$
  • \$\begingroup\$ First of all welcome to PPCG! Your code-golfed code uses the i both as parameter and in the for-loop, so I would change the parameter to something else like a. Also, you can golf it some more by removing the int in front of the j and add it to the int i. So like this: BufferedImage t(BufferedImage a){for(int i=-1,j;++i<a.getWidth();)for(j=-1;++j<a.getHeight();)a.setRGB(i,j,a.getRGB(i,j)+1118481);return a;}. Also, have a look at this post: Tips for golfing in Java. :) \$\endgroup\$ – Kevin Cruijssen Aug 2 '16 at 9:04
  • \$\begingroup\$ You're not setting the colors correctly. By adding 0x111111 to the RGB values, you have the possibility of overflow from one hexadecimal place to the next. For example, #49de5f is supposed to become #5aef60, not #5aef70. \$\endgroup\$ – kamoroso94 Aug 2 '16 at 18:55
0
\$\begingroup\$

R, 302 bytes

Far from perfect, but here goes:

a<-as.raster(imager::load.image(file.choose()));l=letters;n=as.numeric;d=a;f=function(x){if(suppressWarnings(is.na(n(x)))){if(x=="F")"0" else{l[match(x,toupper(l))+1]}}else{if(x==9)"A" else as.character(n(x)+1)}};for(o in 1:90){for(g in 1:200){for(h in 2:7){substr(d[o,g],h,h)<-f(substr(a[o,g],h,h))}}}

Explanation:

a<-as.raster(imager::load.image(file.choose()));  //chooses file
l=letters;n=as.numeric;d=a;                      //shortens some names and creates
                                                   duplicate variable to modify

f=function(x){                                //creates a function that takes in a
   if(suppressWarnings(is.na(n(x)))){            character type and checks whether it is
      if(x=="F")"0"                              a number or letter.
      else{l[match(x,toupper(l))+1]}}            Returns the converted letter or number.                               
   else{                                   
      if(x==9)"A"                         
      else as.character(n(x)+1)}         
};                                      

for(o in 1:90){                       //loops through every single hex digit and applies
   for(g in 1:200){                     the converting function, modifying the duplicate
      for(h in 2:7){                    variable. Now you have 2 images =D

         substr(d[o,g],h,h)<-f(substr(a[o,g],h,h))

      }
   }
}

a beautiful rainbow

\$\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.