function numberOfBalls (black, white) {
return black + white;
};
function costOfBalls (black, white) {
return 100 * numberOfBalls;
};
const totalBallCost = costOfBalls(10, 20);
console.log(totalBallCost)
Hello, Javascript newb here. The code above keeps logging NaN to the console; I can't
see what I'm doing wrong ... could anyone help ? I've tried it multiple consoles, I've tried changing the parameters & the function names while nothing seems to help.
You miss function call at the position numberOfBalls
Use next code:
function numberOfBalls (black, white) {
return black + white;
};
function costOfBalls (black, white) {
return 100 * numberOfBalls(black, white);
};
const totalBallCost = costOfBalls(10, 20);
console.log(totalBallCost)
Related
I am trying to convert this hash color code #159424 (GREEN-COLOR) to more darken and lighten programmatically. How to do this please help?
make green color darker
toDarkColor(String hashColor){
// how to convert that hash string to make green color darker?
}
make green color lighter
toLightColor(String hashColor){
// how to convert that hash string to make green color lighter?
}
For people who want to darken or lighten Color instead of hex string
// ranges from 0.0 to 1.0
Color darken(Color color, [double amount = .1]) {
assert(amount >= 0 && amount <= 1);
final hsl = HSLColor.fromColor(color);
final hslDark = hsl.withLightness((hsl.lightness - amount).clamp(0.0, 1.0));
return hslDark.toColor();
}
Color lighten(Color color, [double amount = .1]) {
assert(amount >= 0 && amount <= 1);
final hsl = HSLColor.fromColor(color);
final hslLight = hsl.withLightness((hsl.lightness + amount).clamp(0.0, 1.0));
return hslLight.toColor();
}
// usage
final lightRed = lighten(Colors.red);
final darkBlue = darken(Colors.blue, .3);
Live Demo
Color accurate solution with no plugin
The accepted answer changes the tint of colors when darkening (the tint is more saturated). Also its lightening function produces pure white with an amount of 0.3 for some colors although white should only be reached with an amount of 1.
The two following methods produce shades of the base color that seem 'darker' or 'lighter' without changing the tint.
import 'package:flutter/material.dart';
/// Darken a color by [percent] amount (100 = black)
// ........................................................
Color darken(Color c, [int percent = 10]) {
assert(1 <= percent && percent <= 100);
var f = 1 - percent / 100;
return Color.fromARGB(
c.alpha,
(c.red * f).round(),
(c.green * f).round(),
(c.blue * f).round()
);
}
/// Lighten a color by [percent] amount (100 = white)
// ........................................................
Color lighten(Color c, [int percent = 10]) {
assert(1 <= percent && percent <= 100);
var p = percent / 100;
return Color.fromARGB(
c.alpha,
c.red + ((255 - c.red) * p).round(),
c.green + ((255 - c.green) * p).round(),
c.blue + ((255 - c.blue) * p).round()
);
}
Example: darken a color by 15%.
final Color darkerGreen = darken(Color(0xFF159424), 15);
If starting from a Hex String value as OP asked, use J.M. Taylor' solution:
Color hexToColor(String code) {
return Color(int.parse(code.substring(0, 6), radix: 16) + 0xFF000000);
}
final Color darkerGreen = darken(hexToColor('#159424'));
Note: it's for Flutter projects as it uses the material's Color class.
My solution based on https://stackoverflow.com/a/58604669/7479173
extension ColorBrightness on Color {
Color darken([double amount = .1]) {
assert(amount >= 0 && amount <= 1);
final hsl = HSLColor.fromColor(this);
final hslDark = hsl.withLightness((hsl.lightness - amount).clamp(0.0, 1.0));
return hslDark.toColor();
}
Color lighten([double amount = .1]) {
assert(amount >= 0 && amount <= 1);
final hsl = HSLColor.fromColor(this);
final hslLight =
hsl.withLightness((hsl.lightness + amount).clamp(0.0, 1.0));
return hslLight.toColor();
}
}
with this one can simply:
Colors.red.darken()
Colors.red.lighten()
Colors.red.lighten(0.1)
this works on any colors as long as you import the extension.
You can use tinycolor package:
TinyColor.fromString("#159424").darken(10).color
Edit:
You can convert Color back to hex string like this:
String toHex(Color color) {
return "#${color.red.toRadixString(16).padLeft(2, "0")}"
"${color.green.toRadixString(16).padLeft(2, "0")}"
"${color.blue.toRadixString(16).padLeft(2, "0")}";
}
or if you want opacity/alpha:
String toHex(Color color) {
return "#${color.alpha.toRadixString(16).padLeft(2, "0")}"
"${color.red.toRadixString(16).padLeft(2, "0")}"
"${color.green.toRadixString(16).padLeft(2, "0")}"
"${color.blue.toRadixString(16).padLeft(2, "0")}";
}
I used withLightness method of HSLColor to lighten the color.
HSLColor.fromColor(Colors.red).withLightness(0.95).toColor()
Since some parts of TinyColor seem broken, and I only really needed lighten and darken, NearHuscarl's answer was perfect for me.
However, it was missing one part that was necessary to completely answer the original question, which was converting hash color code (declared as a String) to Color.
To do that, you can use this:
Color hexToColor(String code) {
return Color(int.parse(code.substring(0, 6), radix: 16) + 0xFF000000);
}
The above is not my code, but something I learned from a tutorial here.
Then just combine that with NearHuscarl's code to get the desired effect:
final Color darkerGreen = darken(hexToColor('#159424'));
One liner with built-in method:
Color.lerp(myColor, Colors.white, 0.4) // 0 = keep as is, 1 = white
I was trying to make a fullscreen window in OpenCV and the functions provided in OpenCV are working except there is a white edge in the left border and top border. After spending time in investigating the code in OpenCV, I found out that the function getWindowImageRect is somehow returning a value that is 1px more in x, y and 1px less in width and height than the function that I have wrote.
Okay, and here is the question. Why are the two functions returning different values?
Here is the code I have found in OpenCV:
CvRect cvGetWindowRect_W32(const char* name)
{
CvRect result = cvRect(-1, -1, -1, -1);
CV_FUNCNAME( "cvGetWindowRect_W32" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT; // keep silence here
RECT rect;
GetClientRect(window->hwnd, &rect);
{
POINT pt = {rect.left, rect.top};
ClientToScreen(window->hwnd, &pt);
result = cvRect(pt.x, pt.y, rect.right - rect.left, rect.bottom - rect.top);
}
__END__;
return result;
}
This is the code that I have wrote in Python, which I thought will give me the same result:
def getWindowRect(name)
hwnd = win32gui.FindWindow(None, name)
rect_l, rect_t, rect_r, rect_b = win32gui.GetClientRect(hwnd)
pt_x, pt_y = win32gui.ClientToScreen(hwnd, (rect_l, rect_t))
result = (pt_x, pt_y, rect_r - rect_l, rect_b - rect_t)
return result
The result of using the two above functions give me output like this:
(1, 1, 2559, 1439)
(0, 0, 2560, 1440)
So I wonder if any one can tell me why is this happening?
Note that the test is performed under Windows 10.
I'm using the NVD3 library to draw a graph. I am using the interactive guideline and for some reason after I update the chart data + graph, the tooltip pointers stay at the old position.
When I update the data I do this:
chartData.datum(data).call(chart);
Everything updates fine except the position of the pointers of the tooltip. They seem to get stuck at the old position. I also tried calling this:
chart.update();
I noticed that when resizing my window and calling chart.update(), the pointers are set to the right position.
Someone any idea how to fix this?
Found it! I apparently had to add transition().duration(500);
So in order to update the graph I call this:
chartData.datum(data)
.transition().duration(500)
.call(chart);
Can you post a Little More description about the problem ... Becuase Previously i was having the Same .. that override the nvd3 tooltip function
`this._nvctp = nv.tooltip.calcTooltipPosition;
nv.tooltip.calcTooltipPosition = this.calcTooltipPosition;
calcTooltipPosition : function() {
this.findTotalOffsetTop = function(a, b) {
return 0;
};
this.findTotalOffsetLeft = function(a, b) {
return 0;
};
arguments[0] = [ window.event.pageX, window.event.pageY ];
var p = nvk.tooltip._nvctp.apply(this, arguments);
p.style.left = (window.event.clientX - (p.clientWidth / 2))
+ 'px';
p.style.top = (window.event.clientY - (p.clientHeight * 1.2))
+ 'px';
p.style.opacity = 1;
p.style.position = 'fixed';
return p;
}
I'm trying to set the fill opacity of a vector feature in OL3 and can't figure out how to do it if I have a hexadecimal value...I've seen examples with rgba. Any suggestions?
Here's what I have:
style : function(feature, resolution) {
return [new ol.style.Style(
{
stroke : new ol.style.Stroke(
{
color : feature.get('color'),
width : 2
}),
fill : new ol.style.Fill(
{
color : feature.get('color'), //'#FF0000'
opacity : 0.2 // I thought this would work, but it's not an option
})
})]
}
This is late but might help someone.
Using rgba property is also possible.
fill: new ol.style.Fill({color: 'rgba(255, 255, 0, 0.63)'}),
You can use the ol.color.asArray function. That function converts color strings to color arrays.
So this is what you can use:
var hexColor = feature.get('color');
var color = ol.color.asArray(hexColor);
color = color.slice();
color[3] = 0.2; // change the alpha of the color
slice() is used to create a new color array. This is to avoid corrupting the "color strings to color arrays" cache that the ol.color.asArray function maintains.
See http://openlayers.org/en/master/apidoc/ol.color.html?unstable=true#asArray.
import ol_color from 'ol/color';
colorWithAlpha(color, alpha) {
const [r, g, b] = Array.from(ol_color.asArray(color));
return ol_color.asString([r, g, b, alpha]);
}
I'm trying to stop fading in/out my movieclip.
I'll explain: I've integrated my swf in an HTML page with a dropdown list. When i choose an item from this list a javascript function it's called.This function execute a callback to a function in my swf file that fade in/out an image drawn at runtime (depending on the item selected in the dropdown list). When I choose another element I want the previuos item stops fading and the new starts.
This is my fading function:
function fadeIn(h){
if (eval(h)._alpha<100) {
eval(h)._alpha += 20;
}
else {
clearInterval(fadeInterval);
setTimeout(startOut, 500, h);
}
}
function fadeOut(h) {
if (eval(h)._alpha>0) {
eval(h)._alpha -= 20;
} else {
clearInterval(fadeInterval);
setTimeout(startIn, 100, h);
}
}
function startOut(h) {
fadeInterval = setInterval(fadeOut, 1, h);
}
function startIn(h){
fadeInterval = setInterval(fadeIn, 1, h);
}
function flashing(h){
var bname;
bname = "planGroup.singleObject." + h;
eval(bname)._alpha = 0;
fadeInterval = setInterval(fadeIn, 1, bname);
}
I tried with clearInterval(fadeInterval), but this doesn't always work, tried with my_mc.stop() but this doesn't work either.
I tried also to set a variable count that execute the fading olny 5 times, and this work unless I change the item in the drowpdown list before the function complete.
Any ideas?? Hope it was clear!
Thanks
If anyone cares i solved with the Tween class!! All those functions were replaced by one line of code:
function fadeTo(clipName, fadeValue){
new mx.transitions.Tween(eval(clipName), "_alpha", mx.transitions.easing.Regular.easeOut, eval(clipName)._alpha, fadeValue, 1, true);
}