Skip to main content
varxtis
Inspiring
October 11, 2021
Answered

Method or plugin for rounding grayscale objects to the nearest quarter percent?

  • October 11, 2021
  • 3 replies
  • 1370 views

I have a grayscale mosaic consisting of 75x75 tiles, created by Create Object Mosaic. Each tile/object ranges from 0% - 100%. I want to round each of thos tiles/objects to their closest quarter percent, 0%, 25%, 50%, 75% or 100%. Meaning if the object is 15%, I want it to be rounded to 25%. If it's 57% then rounded to 50%, and so on. Is there a method or a plugin that allows me to take all the tiles and and have them rounded automatically? I tried doing this by using the select same method, but no matter if I use Same > Appearance, Same > Color & Stroke, or Same > Fill Color, there are no other objects that also become selected.

 

I'd deeply appreciate any time and help you may be able to give.

This topic has been closed for replies.
Correct answer femkeblanco

See if this works for you.

 

// select group
var sel = app.selection[0];
for (var i = 0; i < sel.pathItems.length; i++) {
    var fill = sel.pathItems[i].fillColor;
    if (fill.gray >= 0 && fill.gray <12.5) {
        fill.gray = 0;
    } else if (fill.gray >= 12.5 && fill.gray <37.5) {
        fill.gray = 25;
    } else if (fill.gray >= 37.5 && fill.gray <62.5) {
        fill.gray = 50;
    } else if (fill.gray >= 62.5 && fill.gray <87.5) {
        fill.gray = 75;
    } else {
        fill.gray = 100;
    }
}

3 replies

Kurt Gold
Community Expert
Community Expert
October 25, 2021

That's a plain and very useful script, Femke.

 

Thanks for sharing it.

 

As well as you already requested, I would also welcome some more clarification from varxtis.

 

femkeblanco
Legend
October 15, 2021

It should be doable with a simple script.  What type of color is being used?  If you select your mosaic and run this line, what does the message read? 

 

alert(app.selection[0].pathItems[0].fillColor);

 

varxtis
varxtisAuthor
Inspiring
October 17, 2021

Thank you! Sorry for the delayed response. grayscale confirmed as [GrayColor]

femkeblanco
femkeblancoCorrect answer
Legend
October 18, 2021

See if this works for you.

 

// select group
var sel = app.selection[0];
for (var i = 0; i < sel.pathItems.length; i++) {
    var fill = sel.pathItems[i].fillColor;
    if (fill.gray >= 0 && fill.gray <12.5) {
        fill.gray = 0;
    } else if (fill.gray >= 12.5 && fill.gray <37.5) {
        fill.gray = 25;
    } else if (fill.gray >= 37.5 && fill.gray <62.5) {
        fill.gray = 50;
    } else if (fill.gray >= 62.5 && fill.gray <87.5) {
        fill.gray = 75;
    } else {
        fill.gray = 100;
    }
}
Ton Frederiks
Community Expert
Community Expert
October 11, 2021

Maybe playing with the tolerance setting of the Magic Wand Tool will help?

varxtis
varxtisAuthor
Inspiring
October 15, 2021

Thank you for the reply. I was trying to find any and all information I could regarding the Wand before replying, but it's frustrating. It's like there's so much obvious potential there for the wand, but it's so limited. I wish I could adjust Brightness range instead of tolerance. I also tried to mess with "Recolor Artwork", but thats excruciating as it's picking up over 2500 "color" viariations. Program keeps wanting to crash. I wish I could have 5 objects or swatchese that I could go based off, maybe eye-drop, and it just selects a determined range 1-12.5%, 12.6%-25%, etc... then when that range is selected, I change it to what I need.

There has got to be a way to do this. I like the Wand suggestion, but it just doesn't take brightness into considereation.