What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Photoshop Scripts - Help with my ratios


bungdaddy

Member
Messages
6
Likes
0
So I'm out of my league on this math problem. I'll try to explain as simply as possible. Any help would be awesome. This is a Photoshop extension that I am writing, but it's just basic javascript.

I have a template that is built on a 4200 x 4200 square. That script draws squares based on coordinates contained within that template {L,R,T,B}. The template can be applied to any document size. If that document size is not square, my ratios are off. Any ideas on the math behind this?

Thanks for any help and if I am not clear or you need more info let me know!


Code:
[FONT=lucida console][COLOR=#0B374D]var wd = width of template[/COLOR][/FONT]
[FONT=lucida console]var hd = height of template

var maxWidth = (activeDocument.width);    //width of active document
var maxHeight = activeDocument.height;  //height of active document

var ratioRec = wd/hd; //aspect ratio of template  
var ratioView = maxWidth/maxHeight; //aspect ratio of document

var ratio = ratioView/ratioRec;

var ratioW = maxWidth/wd; 
var ratioT = (maxHeight/hd); 

//Sizing variables, this is where the problem is
var left     = (left * ratioW);
var right    = (right  * ratioW) ;
var top     = (top * ratioT); 
var bottom = (bottom * ratioT) ;[/FONT][FONT=Droid Serif]
[/FONT]
 
I wonder if you could explain what you are trying to do.
A picture would help.
The code you posted does not make sense at all. It is full of errors.
IE:
Code:
[FONT=Lucida Console]var left     = (left * ratioW);
[/FONT]
left is still undefined and you are trying to times that by ratioW, same goes for the others.
Getting the documents width and height, you need more code unless you have set the rulerunits else where.
Code:
[FONT=lucida console][COLOR=#0b374d]var wd = width of template[/COLOR][/FONT]
does not make any sense, if its a template isn't it a fixed size?
 
Thanks Paul, your right my post doesn't contain enough information. Left, Right, Top, and Bottom are being pulled from the template file. So for an example:

Template1 will provide this info -- MakeLayerMask(uniqueLayerName(),420,1240,2060,2880);
The numbers are the coordinates for (Left, Top, Right, Bottom) and the template is based on a file being 4200x4200

What I wish to do is take that template and apply it to any document size, but adjust those coordinates based off the ratio of the template:activeDocument.

Thanks for allowing me to clear this up (hopefully)
 
Ok, I understand the concept, but wouldn't it be easier to work out the percentage for the left, top,right and bottom, so that you can use the same percentages on any document.
 
If I can work out the percentages based on my existing data. I can't rewrite the templates at this point. I'll have to think about that one. If you have any suggestions on it, my ears are open!!

Thanks again
 
Went with your percentage suggestion and this is what I came up with. This is everything. Here's what I'm running into now. As long as my document is square, I'm good, but if I go rectangle, well my proportions are off depending on if the x or y axis is longer.


Code:
var arrDoc = arrData[4].substring(arrData[4].indexOf('(')+1, arrData[4].indexOf(')'));
     arrDoc = arrDoc.split(',');
                            
 var wd     = parseFloat(arrDoc[0]); //width of template
 var hd     = parseFloat(arrDoc[1]); // height of template
 var solution = parseFloat(arrDoc[2]); //resolution of template
 var maxWidth = parseFloat(activeDocument.width)/2;    //width of active document divided in half
 var maxHeight = parseFloat(activeDocument.height);  //height of active document
                        
  for (var i = 0; i < arrsubstr.length; i++ ) {
  var dataRec = arrsubstr[i].substring(arrsubstr[i].indexOf('(')+1, arrsubstr[i].lastIndexOf(')')).split(',');
                                                      
  var left     = dataRec[1];
  var top     = dataRec[2];
  var right     = dataRec[3];
  var bottom     = dataRec[4];
  
  var strtRulerUnits = app.preferences.rulerUnits; 
  var strtTypeUnits = app.preferences.typeUnits; 
  app.preferences.rulerUnits = Units.PIXELS; 
  app.preferences.typeUnits = TypeUnits.PIXELS;
                                
   var perc_tempLeft = dataRec[1]/wd
   var perc_tempTop = dataRec[2]/hd
   var perc_tempRight = dataRec[3]/wd
   var perc_tempBottom = dataRec[4]/hd


   var left = (maxWidth * perc_tempLeft);
   var right = (maxWidth * perc_tempRight);
   var top = (maxHeight * perc_tempTop);
   var bottom = (maxHeight * perc_tempBottom);
                       MakeLayerMask(uniqueLayerName(),parseFloat(left),parseFloat(top),parseFloat(right),parseFloat(bottom));        
                        }
 
Yes it will be.
I think you need to re-think what the final item should look like, as you have found this method just won't work.
Maybe you should be working out a 2:3 ratio area that will fit the current document, also if it should be portrait or landscape.
 


Write your reply...

Back
Top