What is "index" in NSGradient getColor:location:atIndex? - objective-c

In the following NSGradient method:
- (void)getColor:(NSColor **)color location:(CGFloat *)location atIndex:(NSInteger)index
What is index? The documentation says index of the colour I want. But I thought the point of this method is that it tells YOU the colour to use for a particular location ? I don't know the index.
I want to define a gradient (colors and locations), and given a float return the corresponding colour.
Does anyone have an example where a gradient is defined, and this method is called ?

As the docs say,
This method returns the color stop information that was used to create the receiver. It does not return the interpolated color values at any point along the gradient.
The index is into the initial array of colors that you used to create the gradient. The location parameter isn't one that you pass in; it's an out parameter like color, and will give you the location (which you also specified when creating the gradient) associated with the color stop at the index you pass.
It sounds like you're looking for interpolatedColorAtLocation:

Related

How can I pass a variable to a ggtern vertex label?

So I want the vertex label to update automatically when a function argument is passed to a plotting function. All the exampoles I've seen involve setting a fixed label? Is there a way to do this?

How to fill available tableview width with columns?

When tableview is created, the default behaviour of columns is to leave a lot of empty space. Is there a way to automatically fill the space?
One solution is described here:
JavaFX 2 Automatic Column Width
but this seems a little cumbersome.
TornadoFX comes with an advanced column resize policy called SmartResize. You assign it to a TableView like this:
columnResizePolicy = SmartResize.POLICY
By default it tries to do something useful depending on the data, and it will assign any left over width to the last column.
You can configure resizing options per column by calling the appropriate configurator function. For example, to tell the policy to give remaining width to a given column:
column("Name", Person::nameProperty).remainingWidth()
You can configure multiple columns to receive remaning width, so that they will share the remaining width between them.
contentWidth() will make sure the column width fits the content, with optional additional padding: contentWidth(padding = 50.0).
Most resize options also take an optional useAsMin parameter which will enforce the given setting as a minimum width for the column. useAsMax does the same for max width. You will get a fixed size by setting both useAsMin and useAsMax or specifying the fixedWidth(width) option.
You can distribute space using the weigthedWidth(weightNum) function, or even the pctWidth(pctNum) function. All these modes can be combined
Please see the TornadoFX Guide chapter about the resize policy for more information:

Calling a mixin with a return value twice in the same scope

I'm building a LESS file containing a list of color variables that are used through our project. Most of these colors are simple colors (red, blue...), but I want a few of them to be automatically "computed" from the other colors. For example, I have made a mixin to automatically create a color that "stands out" from another color. Following the LESS documentation, I "simulate" a return value by defining a new variable in the mixin, so that I can use it next.
.stand-out-color(#baseColor, #rate) when (lightness(#baseColor) >= 50%) {
#standOutColor: darken(#baseColor, #rate);
}
.stand-out-color(#baseColor, #rate) when (lightness(#baseColor) < 50%) {
#standOutColor: lighten(#baseColor, #rate);
}
.stand-out-color(red, 10%);
#my-first-color: #standOutColor; // A slightly different red, perfect
.stand-out-color(blue, 10%);
#my-second-color: #standOutColor; // The same slightly different red, not the blue I expected
Unfortunately, the return value is written after the first call, and never changed afterwards.
After reading the documentation again, I got this part: "There is only one exception, variable is not copied if the caller contains a variable with the same name (that includes variables defined by another mixin call)."
So it works as it should, but I don't have any idea how to achieve what I want, which is a function I can call more than once within the same scope. As I'm defining a list of global variables, I don't think I can play with scopes to avoid this behavior. Is there any other way to achieve this?
Here's the solution, thanks to seven-phases-max's comment to my question. It solves my initial problem, which is that I needed to compute slightly darker/lighter color variables depending on another variable.
The contrast function of LESS solves my issue, by letting me specify which color to take depending if a color is "dark" or "light".
Applied to my example:
#my-first-color: contrast(#original-color, darken(#original-color, 10%), lighten(#original-color, 10%));

Ellipsize NSTextField?

Is there a way to ellipsize the text content of an NSTextField, instead of truncate?
So instead of:
The quick brown fox jumped over
It would say:
The quick brown fox jumped ...
I can't find anything in the documentation for this. What am I missing?
UPDATE:
Is there any way to ellipsize AND wordwrap--in other words, have multiple lines and ellipsize the last?
So this is a great question! Even though its exposed in IB, its not a property on the view or any subview. Instead its buried as a property on the cell used by NSTextField. So if you ask that object for its cell, you can then read or set the value: lineBreakMode.
Look in the class description for NSCell for all the options - truncateCenter is one (to get center ellipsis).
EDIT: the following thoughts were prompted by the updated question. Personally, I think trying to get that google code is way overkill and perhaps you can do something less complex by creating a mini-custom textField.
create a customer NSView object and give it a string and font property and some methods related to the actions below, and perhaps even a width property
essentially the idea is to use the Cocoa NSString category that lets you determine the length of string (and probably its height) from a string/font combo (I use this in UIKit, did use equivalents in Cocoa, but its been a while...)
one of your view's methods will be 'calculate'. When you get this, covert the string to an array of words using a single space as the separator (or make it more complex). Then, start computing the length of the drawn string, taking the first work, append a space and the second, etc, until you find you have exceeded he width. This is your first line string.
continue doing this for the number of lines that you want to draw (2? 3?). Calculate the length of the unicode char that does ... - its option semicolon as I recall - and keep that around.
in the last line, keep adding words until you exceed the length, and then back up a word at a time, verifying that the last appended strings string (minus a trailing space) but with the '...' char will fit in the space.
you can make this fancier by adding padding around the border etc.
once the calculation is finished (and you of course cache all the bits of the answer), your view is prepared when it gets 'drawRect:...'. You position at (0, bounds.size.height - 21) and draw the first segment, then move down 21 points and draw the second line, etc.
If I were to code this I would plan on 2-4 hours - its not trivial, perhaps the logic is a bit complex, but its straightforward. Good luck!
Although the correct answer was not accepted in this old question - scroll to the last answer - it includes the code Truncate the last line of multi-line NSTextField
The solution includes the use of ellipses and line wrapping combined.

NHibernate - how to achieve this n:m relation?

I have two objects: Color and Palette. Each palette can get a range of colors assigned. That is what I am able to configure out with Fluent NHibernate without any problem.
But: the assignment of colors is only a constraint. If no color is assigned the palette should have every color which is available in the database.
My approach would be to check if Palette.Colors.Count() is 0, and if so, to fetch all the colors by ColorRepository.GetAllColors() and assign them manually to the palette.
Is there another "built-in" way?
It will be difficult (read: impossible) for NHibernate to map something that the database does not reflect.
You would get a huge headache if you assigned all available colors to the palette if no color is assigned. Before persisting you would have to empty the list, so that you do not end up with all those colors in the PaletteColorAssociation table.
The way I understand you, I see two options here:
1) Turn the whole thing around. What I mean is, do not save the colors that are assigned but those that are not assigned. Make that a PaletteColorExclusion table. Of course there are drawbacks to this: 1) If you add a new color to your Colors table, then that color will implicitly be part of every palette and 2) If your general rule is to only have a fraction of the available colors assigned to a palette, you will end up with more rows in the table that you would need.
I do not recommend that approach. It is only meant to show a way how to reflect that logic in the DB.
2) A modification of your apporach seems like the better idea: Do not put that logic within the Palette class but in your business logic classes. Example:
public ReadOnlyCollection<Color> GetColorsForPalette(Palette p)
{
if (p.Colors.Count() != 0)
{
return new ReadOnlyCollection<Color>(p.Colors);
}
else
{
return new ReadOnlyCollection<Color>(ColorRepository.GetAllColors());
}
}
I did not test that code and I put ReadOnlyCollection there to avoid the temptation of altering the collection, since it may not be the palette's colors.