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 6991721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:32:26+00:00 2026-05-27T19:32:26+00:00

I am having some trouble converting a a hexcode to an NSColor. Note this

  • 0

I am having some trouble converting a a hexcode to an NSColor. Note this is for a Mac App (hence the NSColor instead of UIColor). This is the code I have so far:

- (NSColor *) createNSColorFromString:(NSString *)string {
NSString* hexNum = [string substringFromIndex:1];
NSColor* color = nil;
unsigned int colorCode = 0;
unsigned char red, green, blue;
if (string) {
    NSScanner* scanner = [NSScanner scannerWithString:hexNum];
    (void) [scanner scanHexInt:&colorCode];
}
red = (unsigned char) (colorCode >> 16);
green = (unsigned char) (colorCode >> 8);
blue = (unsigned char) (colorCode);
color = [NSColor colorWithCalibratedRed:(float)red / 0xff green:(float)green / 0xff blue:(float)blue / 0xff alpha:1.0];
return color;

}

Any help would be appreciated.

  • 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-27T19:32:27+00:00Added an answer on May 27, 2026 at 7:32 pm
    + (NSColor*)colorWithHexColorString:(NSString*)inColorString
    {
        NSColor* result = nil;
        unsigned colorCode = 0;
        unsigned char redByte, greenByte, blueByte;
    
        if (nil != inColorString)
        {
             NSScanner* scanner = [NSScanner scannerWithString:inColorString];
             (void) [scanner scanHexInt:&colorCode]; // ignore error
        }
        redByte = (unsigned char)(colorCode >> 16);
        greenByte = (unsigned char)(colorCode >> 8);
        blueByte = (unsigned char)(colorCode); // masks off high bits
    
        result = [NSColor
        colorWithCalibratedRed:(CGFloat)redByte / 0xff
        green:(CGFloat)greenByte / 0xff
        blue:(CGFloat)blueByte / 0xff
        alpha:1.0];
        return result;
        }
    

    It doesn’t take alpha values into account, it assumes values like “FFAABB”, but it would be easy to modify.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble converting some code from VB6 to VB.NET (I don't have
Im having some trouble writing a getstring function, this is what I have so
I am having some trouble with this code . The problem is when i
I'm having some trouble converting the following code from c++ to c# because of
I am having some trouble converting an old project from VS6 to VS2005. At
I am having some trouble with the Google Maps API . I have an
I have to port some C# code to Java and I am having some
I'm having trouble with some valarray function pointer code: double (*fp)(double) = sin; valarray<double>
I am converting some Objective-C++ code into plain Objective-C and I am having some
I'm having trouble converting an array into correctly nested HTML. Assuming I have an

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.