Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6604155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:06:59+00:00 2026-05-25T19:06:59+00:00

I’m using an NSGradient for some stuff and at some point I need to

  • 0

I’m using an NSGradient for some stuff and at some point I need to add/change/delete colors. Now NSGradient is immutable as far as I know so the only way I know how to do this is by creating a new gradient and changing the colors and their locations in the process. I have had no problem with this approach for adding colors and for changing the colors’ position but for deleting colors something weird happens.

Here’s the function that deletes a color from the gradient ‘gradient’ being given its index:

- (void)_deleteColorAtIndex: (NSInteger)colorIndex
{
    if (!([self.gradient numberOfColorStops] > 2)) { return; }

    NSMutableArray* newColors = [NSMutableArray arrayWithCapacity: [self.gradient numberOfColorStops] - 1];
    CGFloat locations[[self.gradient numberOfColorStops] - 1];

    NSLog(@"Gradient before:");
    int i;
    for (i = 0; i < [self.gradient numberOfColorStops]; i++) {
        NSColor* color;
        CGFloat location;
        [self.gradient getColor: &color location: &location atIndex: i];
        NSLog(@" - Color: %@ / Location: %.2f", color, location);
    }

    //int i;
    NSLog(@"Adding this to new gradient:");

    // ----------------------------
    for (i = 0; i < [self.gradient numberOfColorStops]; i++) {
        NSColor* color;
        CGFloat location;
        [self.gradient getColor: &color location: &location atIndex: i];

        if (colorIndex != i) {
            [newColors addObject: color];
            locations[i] = location;
            NSLog(@" - Color: %@ / Location: %.2f", color, location);
        }
    }

    self.gradient = [[NSGradient alloc] initWithColors: newColors atLocations: locations colorSpace: [self.gradient colorSpace]];

    // ----------------------------        

    NSLog(@"Gradient after:");
    //int i;
    for (i = 0; i < [self.gradient numberOfColorStops]; i++) {
        NSColor* color;
        CGFloat location;
        [self.gradient getColor: &color location: &location atIndex: i];
        NSLog(@" - Color: %@ / Location: %.2f", color, location);
    }
}

The important code is mainly between the two “// —————————-” thingies. The rest is just for debugging purposes.


The problem

When I run this code with a gradient like |-white—-gray—-black-| and try to delete the second color (gray, colorIndex = 1) I get this gradient: |-whiteblack———|. The last color jumps to position 0.0. Here’s the printed text of that code:

Gradient before:
- Color: NSCustomColorSpace Generic RGB colorspace 1 1 1 1 / Location: 0.00
- Color: NSCustomColorSpace Generic RGB colorspace 0.5 0.5 0.5 1 / Location: 0.50
- Color: NSCustomColorSpace Generic RGB colorspace 0 0 0 1 / Location: 1.00
Adding this to new gradient:
- Color: NSCustomColorSpace Generic RGB colorspace 1 1 1 1 / Location: 0.00
- Color: NSCustomColorSpace Generic RGB colorspace 0 0 0 1 / Location: 1.00 <- cool here
Gradient after:
- Color: NSCustomColorSpace Generic RGB colorspace 1 1 1 1 / Location: 0.00
- Color: NSCustomColorSpace Generic RGB colorspace 0 0 0 1 / Location: 0.00 <- disaster here

Why does this happen??? It makes no sense at all!! (well, come to think of it, almost no bug does until you have found the solution, but still….). Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:07:00+00:00Added an answer on May 25, 2026 at 7:07 pm

    The line

    locations[i] = location;
    

    is incorrect. Here, i is index into the original gradient rather than the index into the new color array. Among other solutions, you could use:

    locations[[newColors count]-1] = location;
    

    or just add a new index counter j that you increment only when adding to the newColors array, and use that index when assigning to locations:

    [newColors addObject:color];
    locations[j++] = location;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.