Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit function #19

Open
schroef opened this issue May 4, 2021 · 5 comments
Open

Limit function #19

schroef opened this issue May 4, 2021 · 5 comments

Comments

@schroef
Copy link

schroef commented May 4, 2021

I bumped into this lib by search for checking color relevance. I got a python example which can order them by relevance using a limit as well. So instead of returning 1 color, it returns an ordered list with a preset limit..

Is such a thing possible?

This example transform the RGB values to a vector list and then uses .length. I dont fully understand/know what .lenght is though in vector lists. I was think 3d points, but since these are its different i guess?!

def relevance(rgb1, rgb2):
        if isinstance(rgb1, RGB) and isinstance(rgb2, RGB):
            relevancelengtn = (__class__.rgb_to_vector(rgb1) - __class__.rgb_to_vector(rgb2)).length
            return 1 - relevancelengtn / __class__.__relevance0
        else:
            return 0

@markusn
Copy link
Owner

markusn commented Feb 20, 2023

I bumped into this lib by search for checking color relevance. I got a python example which can order them by relevance using a limit as well. So instead of returning 1 color, it returns an ordered list with a preset limit..

Is such a thing possible?

This example transform the RGB values to a vector list and then uses .length. I dont fully understand/know what .lenght is though in vector lists. I was think 3d points, but since these are its different i guess?!

def relevance(rgb1, rgb2):
        if isinstance(rgb1, RGB) and isinstance(rgb2, RGB):
            relevancelengtn = (__class__.rgb_to_vector(rgb1) - __class__.rgb_to_vector(rgb2)).length
            return 1 - relevancelengtn / __class__.__relevance0
        else:
            return 0

Hi! could you give me a link to the python code?

@schroef
Copy link
Author

schroef commented Mar 24, 2023

Sorry, i cant seem to find the source nor do i remember where i got this snipprt. Im sorry. Ill try to do some more search.

Below is a ExtendScript version i use to compare CMYK color to a PMS color list in CMYK. This one also has some sort of limit or precision.
The code is from a script by WunderScript,this one is ExtendScript. Used by Adobe apps.
A color object in this case is a CMYK value, which consists of Cyan, Magenta, Yellow and Black

The precs value is some sort of precision input. I believe it is used to check 2 color values against each other and see if they are below this value. The higher the value, the more results it can return. Hope i explain it well enough

function getCols(prec, item){
	//walk through Pantone array to look for any color matches
	if (prec== undefined) {
		//set default precision for color matching
		prec = 1;
	}
	if (inpt == null || inpt.length == 0) {
		//get fill color
		inpt = getDefFill();
	} 
	var num = inpt.split(", ");
	var colen = cols.length;
	var len = names.length;
	var loc = 0; 
	var oneFound = false;
	var matchlist = [];
	var lablist = [];
	for (j=0;j<colen;j++){
		var isbest = compare(num,cols[j],cols[loc],prec);
		if (isbest == true){
			oneFound = true;
			matchlist.push(names[j]);//+" ("+cols[j]+")");
			lablist.push(Arr4[j]); // save LAB values also in a list
			loc = j;
		}
	}

	if(oneFound == true){
		var myCol;
		for (k=0;k<matchlist.length;k++) {
			// // QF - here we need to re color the current object
			// alert(i)
			// alert(matchlist.length)
			// alert(item)
			// var item = doc.pathItems[i];
			try{
				var sw = doc.swatches.getByName(matchlist[k]);
				item.fillColor = sw.color;
                // alert(item.fillColor.typename)
                if (item.fillColor.typename == "SpotColor") item.fillColor.tint = tint;
			} catch(e) {

                // Returns proper LAB values for spot colors
                var sw = addProcSwatch(matchlist[k],false, true, lablist[k], tint);

				// Get PMS swatch from other document
				// var sw = addProcSwatch(matchlist[k],colDoc.swatches);
				// var sw = addProcSwatch(matchlist[k]);
				// alert(matchlist[k])
				// item.fillColor = doc.swatches.getByName(matchlist[k]).color;
				item.fillColor = sw;
				// if (tint) item.fillColor.tint = tint;
			}
		}
	} else {
		if (prec > maxprec){
			//nothing
			newmax = prompt ("No match found within "+maxprec+" deg.\nPress enter to quit, or enter a higher number to keep searching.", "");
			if (newmax.length>0) {
				maxprec = newmax;
				prec++;
				getCols(prec, item);
			}
		} else{
			prec++;
			getCols(prec, item);
		}
	}
} 


function getDefFill(){
	defObj = activeDocument.defaultFillColor;
	if (defObj.typename == "SpotColor"){
		defObj = activeDocument.defaultFillColor.spot.color;
	}
	if(defObj.typename=="RGBColor"){
		return (defObj.red+", "+defObj.green+", "+defObj.blue)
	} else if (defObj.typename=="CMYKColor"){
		return( defObj.cyan+", "+defObj.magenta+", "+defObj.yellow+", "+defObj.black)
	} else {
		alert(defObj.typename+" not understood");
		return ("0,0,0,0")
	}
}


//----------------------------compare--------------------
function compare(arr1, arr2, arrBest, precs){
	var d1 = 0;
	max = arr1.length;
    // colorSpace = space == "RGB" ? Arr2 : Arr3;
	for (w = 0; w < max; w++){
		d1 = diff(arr1[w], arr2[w])
		if(d1 > precs){
			return false
			//if any process color comparison is ridiculously divergent, 
			//don't bother to check the others...	
		}
	}
	return true
}
//----------------------------diff--------------------
function diff(a,b){
	//returns difference between positive numbers
	if (a>b){
		return (a-b)
	} else {
		return (b-a)
	}
}

@schroef
Copy link
Author

schroef commented Mar 24, 2023

Okay, i think i found the source again for that RGB function in python; https://github.com/Korchy/b3d_lib_int/blob/master/rgb.py

@markusn
Copy link
Owner

markusn commented Jun 1, 2023

Okay, i think i found the source again for that RGB function in python; https://github.com/Korchy/b3d_lib_int/blob/master/rgb.py

So basically what you want is a way to match a color to a list of the most relevant colors (up-to-a-limit)? I.e.

Given colors c1, c2, c3, c4. If you set limit to 1 and check relevance for c1 to c2,c3,c4 you get an array with the color with the least diff. If you specify 2 you will get a list with the most relevant color (least diff) first and second the color with the least diff among the remaning colors?

@schroef
Copy link
Author

schroef commented Jun 3, 2023

Yeah something like that. I yave a version already from someone else. But id like to see if there are different methods to tacklle this. The base part is from one of wundes scripts to match pms colors. Ive used this to make a custom cmyk or rgb to oms converted which can be done by script. The rrason is that recolor in illustrator cant be reach by script. Also adobe and pms have parted ways. But i still want to ve able to use it in scripting.

Ive been working on a panel which generates and export logo and its logo types together with different color versions. Its called logo packer, you can see it here l: https://github.com/schroef/logo-packer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants