"Add one" to every color in an image

23

2

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

haykam

Posted 2016-06-19T15:47:47.427

Reputation: 784

What assumptions can we make on input/output format, if we would like to use a programming language designed for hardware/FPGA design? – Reinstate Monica - ζ-- – 2016-06-19T15:52:09.120

@hexafraction I think by default you can take as a file or as hex code iirc. – Rɪᴋᴇʀ – 2016-06-19T16:03:13.220

@hexafraction http://meta.codegolf.stackexchange.com/q/9093/8478

– Martin Ender – 2016-06-19T16:03:37.480

@MartinEnder Thanks. I'm still a bit unclear in regard to output timing due to the nature of Verilog as an HDL; should I pop into the site chat to seek further clarification? – Reinstate Monica - ζ-- – 2016-06-19T16:09:03.560

@hexafraction Either that or ask on meta. – Martin Ender – 2016-06-19T17:43:32.160

5@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). – Martin Ender – 2016-06-19T17:44:40.633

2You 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! – James – 2016-06-19T18:59:03.437

Answers

5

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

Blue

Posted 2016-06-19T15:47:47.427

Reputation: 26 661

Can you provide the output image? – haykam – 2016-06-26T16:03:05.723

7

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.

Martin Ender

Posted 2016-06-19T15:47:47.427

Reputation: 184 808

4

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 !

TuxCrafting

Posted 2016-06-19T15:47:47.427

Reputation: 4 547

Is it necessary to use 0xFF instead of 255? – Yytsi – 2016-06-20T23:23:41.080

@TuukkaX Woops i haven't noticed that thanks you – TuxCrafting – 2016-06-21T07:10:00.810

There is one more 0xFF in there :D – Yytsi – 2016-06-21T07:12:56.123

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. – Yytsi – 2016-06-21T17:00:45.550

@TuukkaX Yes, it can be changed to from PIL import*, but i can't change the Image.open – TuxCrafting – 2016-06-21T17:23:10.403

Oh okay. I'm pretty sure that (h>>8)&255 can be changed to h>>8&255 and (h>>16)&255 to h>>16&255. – Yytsi – 2016-06-21T17:37:45.160

Also, you can save 2 bytes by removing the outer parentheses when you're defining the variable h. – Yytsi – 2016-06-21T17:41:23.980

@TuukkaX Thanks you ! Oh and from PIL import* don't work :c – TuxCrafting – 2016-06-21T18:04:48.217

No problem! It doesn't work since open is an built in function in Python so it meets a collision when importing everything from PIL :( – Yytsi – 2016-06-21T18:10:19.210

You can combine 2 lines with m.putpixel((x,y),(h&255,h>>8&255,h>>16&255)), and 0x111111 can become 1118481. – mbomb007 – 2016-06-21T18:23:49.057

@mbomb007 Thanks you – TuxCrafting – 2016-06-21T18:29:06.947

And some spaces can become tabs. See this tip.

– mbomb007 – 2016-06-21T18:34:51.653

golfing your solution a bit further, I have 216 bytes : from PIL import Image R,m=range,Image.open(input()).convert("RGB") L=m.load() W,H=m.size def X(*p):t=L[p];h=t[0]+(t[1]<<8)+(t[2]<<16)+1118481;L[p]=h&255,h>>8&255,h>>16&255 [X(x,y)for x in R(W)for y in R(H)] m.show() – dieter – 2016-08-02T09:36:20.567

4

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

Reinstate Monica - ζ--

Posted 2016-06-19T15:47:47.427

Reputation: 288

1

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│
└──────┴──────┘

Adám

Posted 2016-06-19T15:47:47.427

Reputation: 37 779

1

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

Jacajack

Posted 2016-06-19T15:47:47.427

Reputation: 501

1

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;}

Roman Gräf

Posted 2016-06-19T15:47:47.427

Reputation: 2 915

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

– Kevin Cruijssen – 2016-08-02T09:04:14.080

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. – kamoroso94 – 2016-08-02T18:55:47.487

0

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

a soft pillow

Posted 2016-06-19T15:47:47.427

Reputation: 131