Compress your code in an image
MATLAB, 81 72 69 bytes
@()image(shiftdim(reshape(evalin('base','char(ans)'),3,1,23)/255,1.))
This creates an anonymous function which can be pasted into the command window and run using ans()
. When run this creates a 23-pixel image (a prime) therefore the most square representation is a simple array of pixels.
Explanation
When pasted into the command window, the anonymous function will automatically assign itself to the variable ans
. Then from within the anonymous function, we use:
evalin('base', 'char(ans)')
which evaluates char(ans)
within the namespace of the command window rather than within the local namespace of the anonymous function. It is therefore able to convert the anonymous function itself into a string representation.
Then we have the following operations which are more straightforward:
%// Reshape the result into a 3 x 1 x 23 matrix where the first dimension is RGB
%// due to column-major ordering of MATLAB
R = reshape(string, 3, 1, 23);
%// Divide the result by 255. This implicitly converts the char to a double
%// and in order for RGB interpreted properly by MATLAB, doubles must be
%// normalized between 0 and 1.
R = R / 255;
%// Shift the dimensions to the left 1 to move the RGB channel values into
%// the third dimension. Note the extra decimal point. This is because it
%// is far shorter to lengthen the code by one byte than to pad the string
%// to give us a length divisible by 3
R = shiftdim(R, 1.);
%// Display the RGB image
image(R);
Javascript(ES6) 324 312 309 Bytes
I'm sure this could be golfed a bit:
f=()=>{s="f="+f;l=s.length/3;c=document.createElement('canvas');for(i=0;++i<l/i;l%i?0:w=i,h=l/w);c.s=c.setAttribute;c.s("width",w);c.s("height",h);(i=(t=c.getContext('2d')).createImageData(w,h)).data.map((a,b)=>i.data[b]=b%4<3?s.charCodeAt(b-~~(b/4)):255);t.putImageData(i,0,0);open(c.toDataURL('image/png'))}
- creates a canvas
- builds image in it
- Opens data url for image in new tab
New lines for readability:
f=()=>{
s="f="+f;l=s.length/3;
c=document.createElement('canvas');
for(i=0;++i<l/i;l%i?0:w=i,h=l/w);
c.s=c.setAttribute;
c.s("width",w);
c.s("height",h);
(i=(t=c.getContext('2d')).createImageData(w,h)).data.map((a,b)=>i.data[b]=b%4<3?s.charCodeAt(b-~~(b/4)):255);
t.putImageData(i,0,0);
open(c.toDataURL('image/png'))
}
Output
Javascript ES6 - 216 bytes
f=( )=>((d=(x=(v=document.createElement`canvas`).getContext`2d`).createImageData(v.width=9,v.height=8)).data.set([..."f="+f].reduce((p,c,i)=>(c=c.charCodeAt(0),[...p,...i%3<2?[c]:[c,255]]))),x.putImageData(d,0,0),v)
Ungolfed:
f=( )=>( // define function f (extra spaces to account for missing pixel + alpha channel calculation)
(d=(x=(v=document.createElement`canvas`). // assign html5 canvas to v
getContext`2d`). // assign graphics context to x
createImageData(v.width=9,v.height=8)). // create & assign ImageData to d
// set width and height of both d and v
data.set([..."f="+f]. // get f's source, convert to array of chars
reduce((p,c,i)=>(c=c.charCodeAt(0), // convert char array to int array
[...p,...i%3<2?[c]:[c,255]]))), // insert alpha values 255
x.putImageData(d,0,0), // draw source RGBA array to canvas
v) // return canvas
Note: f
returns a canvas.
Example run (assumes there's a <body>
to append to):
document.body.appendChild(f())
Should dump the following image to the page (enlarged):