Skip to main content
February 3, 2021
Answered

Assigning Number to Color

  • February 3, 2021
  • 6 replies
  • 3843 views

 

Is there anyway to assign a number to each color to print out a color by number sheet?

This topic has been closed for replies.
Correct answer Chuck Uebele

Okay here's a script that you can try. There are some things you need to do. You need to enlarge the image using precent. I enlarged it 2400%, so that a one pixel square would be 24 px. In the script, you have to enter the hex values of the colors in the art work and the corosponding number that you want displayed. This script takes a long time to run, as it has to sample so many areas. So try it out on a small image first. Here is my test image:

#target photoshop
var doc = activeDocument;

var step = 24;

var hexVal, colorNum, pts

//Enter hex value of numbers and then the number to be displayed into the below array
var cArray = [['000000',1],
['FFFFFF',2],
['017185',3],
['0000FF',4],
['FF0000',5],
['3CFF00',6]];

for (var k=0;k<doc.height;k+=step){
    for( var j=0;j<doc.width;j+=step){
        pts = [j+step/2,k+step/2];
        hexV = getColor ();
        colorNum = findNum()
        makeText ();
        doc.activeLayer.rasterize (RasterizeType.TEXTCONTENTS)
        if(k>0||j>0)(doc.activeLayer.merge())
        }
    }

function makeText(){
    var artLayerRef = doc.artLayers.add()
    artLayerRef.kind = LayerKind.TEXT;
    var textItemRef = artLayerRef.textItem;
    textItemRef.contents =  colorNum
    textItemRef.size =  25
    textItemRef.position= pts
    var tCenter = pts[1]-(doc.activeLayer.bounds[1].value+(doc.activeLayer.bounds[3].value-doc.activeLayer.bounds[1].value)/2);

    
    doc.activeLayer.translate (0, tCenter)
    textItemRef.justification = Justification.CENTER;   
    }

function getColor(){
    doc.colorSamplers.removeAll();
    var cS = doc.colorSamplers.add(pts);
    var rgbC = new SolidColor
    var c1 = cS.color.rgb.hexValue
    return c1
    }

function findNum(){
    for (i=0;i<cArray.length;i++){
        if (hexV == cArray[i][0]){
            return cArray[i][1]; 
            }
        }
    return ' ';
    }
               

6 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
February 4, 2021

Okay here's a script that you can try. There are some things you need to do. You need to enlarge the image using precent. I enlarged it 2400%, so that a one pixel square would be 24 px. In the script, you have to enter the hex values of the colors in the art work and the corosponding number that you want displayed. This script takes a long time to run, as it has to sample so many areas. So try it out on a small image first. Here is my test image:

#target photoshop
var doc = activeDocument;

var step = 24;

var hexVal, colorNum, pts

//Enter hex value of numbers and then the number to be displayed into the below array
var cArray = [['000000',1],
['FFFFFF',2],
['017185',3],
['0000FF',4],
['FF0000',5],
['3CFF00',6]];

for (var k=0;k<doc.height;k+=step){
    for( var j=0;j<doc.width;j+=step){
        pts = [j+step/2,k+step/2];
        hexV = getColor ();
        colorNum = findNum()
        makeText ();
        doc.activeLayer.rasterize (RasterizeType.TEXTCONTENTS)
        if(k>0||j>0)(doc.activeLayer.merge())
        }
    }

function makeText(){
    var artLayerRef = doc.artLayers.add()
    artLayerRef.kind = LayerKind.TEXT;
    var textItemRef = artLayerRef.textItem;
    textItemRef.contents =  colorNum
    textItemRef.size =  25
    textItemRef.position= pts
    var tCenter = pts[1]-(doc.activeLayer.bounds[1].value+(doc.activeLayer.bounds[3].value-doc.activeLayer.bounds[1].value)/2);

    
    doc.activeLayer.translate (0, tCenter)
    textItemRef.justification = Justification.CENTER;   
    }

function getColor(){
    doc.colorSamplers.removeAll();
    var cS = doc.colorSamplers.add(pts);
    var rgbC = new SolidColor
    var c1 = cS.color.rgb.hexValue
    return c1
    }

function findNum(){
    for (i=0;i<cArray.length;i++){
        if (hexV == cArray[i][0]){
            return cArray[i][1]; 
            }
        }
    return ' ';
    }
               

Michael Bullo
Community Expert
Community Expert
February 4, 2021

Awesome work as always Chuck.

Chuck Uebele
Community Expert
Community Expert
February 3, 2021

I'm seeing about how to write a script for it. I had to redraw your image, as it had too much jpg artifacts. What needs to be done is to have the lines a color that isn't used in the art. Is there a final size for the cells, as the script need to make a step for each cell size, including the line width to measure the color and place the text. 

February 3, 2021

I can provide an image made in photoshop (PNG file) if you prefer. On photoshop the pixels are seperated but, there is no visible grid.

 

Final size for cells is not imporatant... I think. Can it not be "blown up" later as a print?

Chuck Uebele
Community Expert
Community Expert
February 3, 2021

Yes, a png would be better, maybe. without the grid line there will be numbers in the background color also. Maybe you want that. For the script to work you have to be consistant with the size of cells, or it would be difficult to have a script tell what the size of a cell it and where to place the number, and also have the number legible.

Chuck Uebele
Community Expert
Community Expert
February 3, 2021

It could be done with a script that reads each color square and references the color against a value list.

February 3, 2021

Where can I read up on doing this? or even pay to have it done?

February 3, 2021

I would like to have just the numbers. I would like to make print out for kids to color by number having the images appear as they go.

Semaphoric
Community Expert
Community Expert
February 3, 2021

Would these all be pixel art? Would you be marking every pixel cell, or just contiguous areas?

February 3, 2021

Yes they are all pixel art. Prefer to have all cells individually marked becaus some of the pictures alternate color pretty frequently.

Michael Bullo
Community Expert
Community Expert
February 3, 2021

Just to be clear, are you looking to replace each block of color within the image with a number? Are these numbers also to appear within a table along with the appropriate color?

February 3, 2021

I would like to have just the numbers. I would like to make print out for kids to color by number having the images appear as they go

Semaphoric
Community Expert
Community Expert
February 3, 2021

Image > Mode > Index Color, and then save the Color Table as an .ACT file. An .ACT file is just a series of 256 three-byte values [RGB] . You can then load this into your Swatches panel. You could construct a document with each swatch to print out, or maybe a script to do it if you had a lot of them.

February 3, 2021

Would this allow me to replace the colors with numbers? I created a 5 color table and saved it.