Visual Studio 2019 change JavaScript styles - javascript

How can I change how Visual Studio styles JavaScript? I'm looking in Tools > Options > Environment > Fonts and Colors and I see that Identifier and Keyword are there , but are those the only two things that can be customized?
function AddNums(num1, num2){
//How do I style comments?
var x = num1 + num2,
return x;
}
There must be more to customize than just 2 things, right?
How do I change properties, plain text, comments, operators, etc?

Related

Get RGB color of coordinates: Adobe Illustrator

I'm using ExtendScript to work on JavaScript for Adobe Illustrator 2015. Is there any way I could get RGB values from a coordinate in the code below?
// declares a document
var doc = app.activeDocument;
// sets x and y coordinates to get color from
var xPosition = 70.0;
var yPosition = 64.0;
This is what needs work:
// gets rgb values
double redValue = doc.getRGBColor(xPosition, yPosition).red;
double greenValue = doc.getRGBColor(xPosition, yPosition).red;
double blueValue = doc.getRGBColor(xPosition, yPosition).red;
I've googled quite a bit and found this.
It doesn't work, though, either because it was posted in 2009, or because it was meant to be in photoshop.
A solution to the problem or translation of that post would be greatly appreciated.
[EDIT: My apologies for giving essentially an AppleScript answer to your ExtendScript question. I was just looking over AS questions and forgot I went to a different section. I can only hope that you are on a Mac. If not, I guess I'll just eat my downvotes and weep.]
There is a workaround. The advantage of it (and part of the workaround nature of it) is that it works in all apps. The downside is that it requires python (which should be on your Mac anyway - fairly easy to install if not), and two pieces of third party software (both free), "checkModifierKeys" and "cliclick". I've been using a script that appears in my script menu for years.
The python part is described here: http://thechrisgreen.blogspot.com/2013/04/python-script-for-getting-pixel-color.html
This script can be saved, made executable and invoked using the AS do shell script command.
and the rest of it, for selecting a point on the screen and for waiting for the control key to be pressed (that's how mine works) is pretty simple.
The basic checkModifierKeys part (which waits until the Control key is pressed) is:
set controlIsDown to false
repeat until (controlIsDown)
set initialCheck to ((do shell script "/usr/local/bin/checkModifierKeys control"))
if initialCheck = "1" then set controlIsDown to true
end repeat
The cliclick part (which gets the coordinates) is:
set xyGrabbed to do shell script "/usr/local/bin/cliclick p"
It may seem like a long way to go for it, but it works great. My version converts the rgb value to hex with this handler, which is useful for my purposes:
to makeHex(theNumber) --was anInteger
--Converts an unsigned integer to a two-digit hexadecimal value
set theResult to ""
repeat with theIndex from 1 to 0 by -1
set theBase to (16 ^ theIndex) as integer
if theNumber is greater than or equal to theBase then
set theMultiplier to ((theNumber - (theNumber mod theBase)) / theBase) as integer
set theResult to theResult & item theMultiplier of ¬
{1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"}
set theNumber to (theNumber - theBase * theMultiplier)
else
set theResult to (theResult & "0")
end if
end repeat
theResult
end makeHex
Yes, this solution doesn't work, because in vector editors such as Illustrator, color is applied to vector item (path) on the whole not to separate pixels. Even if you work with pixel image in illistrator there are no scripting functions to get pixel color.
I think it could be achieved via the rather monstrous solution:
Rasterize the artrboard.
Create Object Mosaic (where one tile ~> one pixel).
Figure out which one of the tiles lies underneath the given coordinates and pick its color.

Compare images in node.js (using e.g. opencv)

What I want to do is to get an image diff using node.js.
Ultimately I want to have a method expecting e.g. two filepaths or image data outputting the subtraction of both. Somehow like the compare function in imagemagick.
Something like:
var comparison_result = compareModule.compare('./image1.png', './image2.png');
Also, I would like to get the position of the spots in the resulting image that mark the differences.
Like this:
comparison_result.forEach(function(difference) {
console.log("A difference occurred at " + difference.x + "|" + difference.y);
});
I installed node-opencv, however I can not find a documentation that maps basic opencv c++ functions to node.js.
The function I would like to use is cvSub.
I would like to avoid js-imagediff as it works with canvas, has a dependency to "cairo" and I am not sure whether I can access the spots because in the documentation it rather seems like it just returns the difference as an image.
I have never tried to calculate per-element difference by cv::addWeighted() but it may work in practice:
var diff = new cv.Matrix(first.width(), first.height());
diff.addWeighted(first, 1.0, second, -1.0);
In native code (C++), this function can be replaced with the expression below:
diff = first*1.0 + second*(-1.0) + 0.0;
p.s.: node-opencv's authors published a sample code for measuring similarity:
node-opencv / examples / dissimilarity.js

Adobe Livecycle cacluations and if statements

my first time posting, and tbh I have very little Xp. I'm using livecycle for Adobe 9 pro, and trying to make a calculation work, and keep getting error messages.
My basic premise I need to do a calculation: enter info in cella, and have result of (cella/2)-5 rounded down, keeping the negative integer come out in another cell. (yes, trying to do my own 3.5 d20 character sheet for ability scores).
In excel I was able to string a slightly more complicated trunc formula of =IF((Cella-10)/2<0,TRUNC((Cella)/2-0.5),TRUNC((Cella-10)/2)), but have no idea what to do in livecycle.
I tried something like this following a tutorial for livecycle, with no avail. honestly noobing it here on all accounts including where notations and variables should be, thanks for the help.
var x = cell1/2-5; if (x <0) {return Math.ceil(x)}; else {return Math.floor(x)}
//cell1 pick from sheet in livecycle using control+click, tried calculation and enter formats on script line. //Do I need a var x for my formula, not sure, some tutorials said yes, others no for live cycle. //not sure where to put { } if at all. // Math.floor and Math.ceil do not show up highlighted blue like other functions do in the livecycle script bar, which I leave in javamode for all cells.
Previous answer has got the math right but it won't work in a LiveCycle event (return statement is invalid in an event.)
Let's assume you have two fields: cellA (where you input numbers), cellB (where the rounded result should display). Replace with the actual names in your sheet as necessary.
In the calculate event for cellB (the field that holds the RESULT) put in this code:
var v = null;
if (cellA.rawValue != null && cellA.rawValue != "")
{
v = cellA.rawValue / 2 - 5;
v = v < 0 ? Math.ceil(v) : Math.floor(v);
}
this.rawValue = v;
The if statement is there so that it won't calculate when you first open the form.
Side note, the javascript editor in livecycle pretty much sucks so don't expect a lot of help with syntax/formatting. I would recommend you download Notepad++ and paste the java code in there, it is more helpful for highlighting some syntax and for checking open/closed parentheses.

Trying to display monitor aspect ratios via html

<SCRIPT language="JavaScript">
height = screen.height;
width = screen.width;
aspect = width/height;
if (width==1920)(height==1200)
{
x="16:10";
}
else if (width==1680)(height==1200)
{
x="16:9";
}
else
{
x="unknown";
}
document.write( x );
</SCRIPT>
This is my exact code. I just started javascript this morning. It's currently displaying nothing.
This is not a correct syntax:
if (width==1920)(height==1200)
Use the && (and) operator to tell if both conditions are true:
if (width==1920 && height==1200)
P.S. For a more general solution to generate the x string, treat width and height as numerator and denominator of a fraction and simplify the fraction by dividing by common prime factors.
Firstly you're using If wrong:
if (width==1920)(height==1200)
Is invalid syntax, if you want to and two or more conditions together use the && operator:
if (width==1920 && height==1200)
Secondly this example will only "work" for two known resolutions, I suggest you check the aspect variable instead of the height and width. Here's an expandable example:
var ratios = [
{name: "16x10", a: 16/10},
{name: "16x9", a: 16/9},
{name: "4x3", a: 4/3}
// etc...
]
var aspect = screen.width/screen.height;
var aspectName = "Unknown";
for(var i = 0; i < ratios.length; i++)
{
var ratio = ratios[i];
if (aspect == ratio.a) {
aspectName = ratio.name;
break;
}
}
document.write(aspectName);
LIVE DEMO
function ar() {
// Greatest Common Divisor algorithm loop:
function gcd(x,y){return !y?x:gcd(y,x%y);}
var s=screen, w=s.width, h=s.height, r=gcd(w,h);
return w/r+':'+h/r; // Return the Aspect-Ratio String
}
document.body.innerHTML = ar();
https://en.wikipedia.org/wiki/Euclidean_algorithm
There is a couple of things that is incorrect in your code.
HTML tags must be written in lowercase
It's not language="JavaScript" but type="text/javascript" (you can avoid this declaration if you're building an HTML5 website)
As others said, if you want two conditions to be true at the same time you have to write width == 1920 && height = 1200
When declaring a variable, use "var" before the name var height = screen.height; You just need it when creating the variable, to use it just call height. Not using "var" will create a global variable but that is for another lesson, just remember it's better to do it this way.
If you're using Chrome, right click on the document and then "inspect". The inspector will pop out. This tool is really powerfull and may seems complex but just read what is written in the "console" tab. You'll eventually know there is an error about your syntax at line x, etc. (The console is available in nearly all browsers but I don't really know how they works on others than Chrome).

Is there a correct way of spacing in Javascript?

In a preliminary technical interview, I was asked to write a simple calculator function in Javascript. My code was passable but he commented on my bad spacing. I wrote something like this:
var calc = function(num1, num2, operand){ //function(... VS function (...
if(operand === 'add'){
return num1 + num2;
} else if(operand === 'multiply'){ // if(...
return num1 * num2;
} else if (operand === 'subtract'){ // if (...
return num1 - num2;
} else {
console.log("Not a valid operand");
};
};
I am a beginner in Javascript looking to learn and maintain good coding habits. I understand the function above would run regardless of my inconsistent spacing, but is there a correct way of spacing Javascript control loops?
Any advice or coding examples will help! Thanks!
He probably thought it was bad because you write in a different style than he does.
A good resource on code style is the Google JavaScript Style Guide.
The key point at the end of the page says:
BE CONSISTENT.
If you're editing code, take a few minutes to look at the code around
you and determine its style. If they use spaces around all their
arithmetic operators, you should too. If their comments have little
boxes of hash marks around them, make your comments have little boxes
of hash marks around them too.
The point of having style guidelines is to have a common vocabulary of
coding so people can concentrate on what you're saying rather than on
how you're saying it. We present global style rules here so people
know the vocabulary, but local style is also important. If code you
add to a file looks drastically different from the existing code
around it, it throws readers out of their rhythm when they go to read
it. Avoid this.
It's all really just a matter of opinion. Your code is perfectly acceptable - slightly inconsistent in places, but valid nonetheless. I personally would write it something like this:
var calc = function (num1, num2, operand) {
if (operand === 'add') {
return num1 + num2;
} else if (operand === 'multiply') {
return num1 * num2;
} else if (operand === 'subtract') {
return num1 - num2;
} else {
console.log("Not a valid operand");
}
};
Well, I'd actually probably use a switch statement but anyway...
Here are a few style guides you might find useful:
http://javascript.crockford.com/code.html
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
http://contribute.jquery.org/style-guide/js/
Of course, none of these are the right answer, but they can all help keep your code clean and maintainable.
Does this work for you?
var calc = function(num1, num2, operand){
return ( new Function( 'return ' + num1 + operand + num2 ) )();
};
And as others already mentioned, there are no white space rules like Python or F# in JavaScript. He might said bad spacing due to below reasons
If you use if() else(), for every comparison value has to retrieved from memory, if switch is used, the value will be retrieved once and do the job. Ofcourse Switch is better in this case.
Using switch is also not necessary in this case. You can use as I have mentioned in answer.

Categories