how to convert hexadecimal to RGB












26















I want to make a conversion from hexadecimal to RGB, but the hexadecimal deal with a string like #FFFFFF. How can I do that?










share|improve this question




















  • 4





    which language?

    – st0le
    Nov 24 '10 at 9:27











  • Can you specify the language?

    – Dr Casper Black
    Nov 24 '10 at 9:32











  • i'm sorry, i've edit it..it is in objective C

    – R. Dewi
    Nov 24 '10 at 9:32






  • 2





    Also have a look at: stackoverflow.com/questions/3723846 and stackoverflow.com/questions/3010216/…

    – Cody Gray
    Nov 24 '10 at 9:46













  • @ceacare yes i am, why?

    – R. Dewi
    Nov 22 '11 at 3:10
















26















I want to make a conversion from hexadecimal to RGB, but the hexadecimal deal with a string like #FFFFFF. How can I do that?










share|improve this question




















  • 4





    which language?

    – st0le
    Nov 24 '10 at 9:27











  • Can you specify the language?

    – Dr Casper Black
    Nov 24 '10 at 9:32











  • i'm sorry, i've edit it..it is in objective C

    – R. Dewi
    Nov 24 '10 at 9:32






  • 2





    Also have a look at: stackoverflow.com/questions/3723846 and stackoverflow.com/questions/3010216/…

    – Cody Gray
    Nov 24 '10 at 9:46













  • @ceacare yes i am, why?

    – R. Dewi
    Nov 22 '11 at 3:10














26












26








26


15






I want to make a conversion from hexadecimal to RGB, but the hexadecimal deal with a string like #FFFFFF. How can I do that?










share|improve this question
















I want to make a conversion from hexadecimal to RGB, but the hexadecimal deal with a string like #FFFFFF. How can I do that?







objective-c ios cocoa-touch hex rgb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 '12 at 18:45









vikingosegundo

48.6k14115162




48.6k14115162










asked Nov 24 '10 at 9:22









R. DewiR. Dewi

1,62283561




1,62283561








  • 4





    which language?

    – st0le
    Nov 24 '10 at 9:27











  • Can you specify the language?

    – Dr Casper Black
    Nov 24 '10 at 9:32











  • i'm sorry, i've edit it..it is in objective C

    – R. Dewi
    Nov 24 '10 at 9:32






  • 2





    Also have a look at: stackoverflow.com/questions/3723846 and stackoverflow.com/questions/3010216/…

    – Cody Gray
    Nov 24 '10 at 9:46













  • @ceacare yes i am, why?

    – R. Dewi
    Nov 22 '11 at 3:10














  • 4





    which language?

    – st0le
    Nov 24 '10 at 9:27











  • Can you specify the language?

    – Dr Casper Black
    Nov 24 '10 at 9:32











  • i'm sorry, i've edit it..it is in objective C

    – R. Dewi
    Nov 24 '10 at 9:32






  • 2





    Also have a look at: stackoverflow.com/questions/3723846 and stackoverflow.com/questions/3010216/…

    – Cody Gray
    Nov 24 '10 at 9:46













  • @ceacare yes i am, why?

    – R. Dewi
    Nov 22 '11 at 3:10








4




4





which language?

– st0le
Nov 24 '10 at 9:27





which language?

– st0le
Nov 24 '10 at 9:27













Can you specify the language?

– Dr Casper Black
Nov 24 '10 at 9:32





Can you specify the language?

– Dr Casper Black
Nov 24 '10 at 9:32













i'm sorry, i've edit it..it is in objective C

– R. Dewi
Nov 24 '10 at 9:32





i'm sorry, i've edit it..it is in objective C

– R. Dewi
Nov 24 '10 at 9:32




2




2





Also have a look at: stackoverflow.com/questions/3723846 and stackoverflow.com/questions/3010216/…

– Cody Gray
Nov 24 '10 at 9:46







Also have a look at: stackoverflow.com/questions/3723846 and stackoverflow.com/questions/3010216/…

– Cody Gray
Nov 24 '10 at 9:46















@ceacare yes i am, why?

– R. Dewi
Nov 22 '11 at 3:10





@ceacare yes i am, why?

– R. Dewi
Nov 22 '11 at 3:10












3 Answers
3






active

oldest

votes


















82














I've just expanded my UIColor Category for you.

use it like UIColor *green = [UIColor colorWithHexString:@"#00FF00"];



//
// UIColor_Categories.h
//
// Created by Matthias Bauch on 24.11.10.
// Copyright 2010 Matthias Bauch. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface UIColor(MBCategory)

+ (UIColor *)colorWithHex:(UInt32)col;
+ (UIColor *)colorWithHexString:(NSString *)str;

@end

//
// UIColor_Categories.m
//
// Created by Matthias Bauch on 24.11.10.
// Copyright 2010 Matthias Bauch. All rights reserved.
//

#import "UIColor_Categories.h"

@implementation UIColor(MBCategory)

// takes @"#123456"
+ (UIColor *)colorWithHexString:(NSString *)str {
const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];
long x = strtol(cStr+1, NULL, 16);
return [UIColor colorWithHex:x];
}

// takes 0x123456
+ (UIColor *)colorWithHex:(UInt32)col {
unsigned char r, g, b;
b = col & 0xFF;
g = (col >> 8) & 0xFF;
r = (col >> 16) & 0xFF;
return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1];
}

@end





share|improve this answer
























  • I know this is old now but I believe that you have to #import <UIKit/UIColor.h> to actual be able to do UIColor(MBCategory) otherwise you get an error saying unable to find @interface UIColor. This could just have been an update since this was written, but it doesn't work without the import.

    – Popeye
    Jul 11 '12 at 14:27













  • It should work in a standard iOS project created by Xcode. The whole UIKit is imported in the precompiled header (ProjectName-Prefix.pch).

    – Matthias Bauch
    Jul 11 '12 at 16:22





















25














//In your header file  

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

//usage
UIColor *color = UIColorFromRGB(0x000000)
//you can also use it inline
[text.textField setTextColor:UIColorFromRGB(0xcccccc)];





share|improve this answer



















  • 2





    I think this is something great :)

    – Munim Dibosh
    Jun 14 '13 at 8:47



















5














Swift




It is very useful




extension UIColor {

convenience init(hexString: String) {
let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int = UInt32()
Scanner(string: hex).scanHexInt32(&int)
let a, r, g, b: UInt32
switch hex.count {
case 3: // RGB (12-bit)
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6: // RGB (24-bit)
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
(a, r, g, b) = (1, 1, 1, 0)
}
self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
}

public func hexStringWithAlpha(_ includeAlpha: Bool) -> String {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
self.getRed(&r, green: &g, blue: &b, alpha: &a)

if (includeAlpha) {
return String(format: "%02X%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255), Int(a * 255))
} else {
return String(format: "%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
}
}

open override var description: String {
return self.hexStringWithAlpha(true)
}

open override var debugDescription: String {
return self.hexStringWithAlpha(true)
}
}


Obj-C



UIColor *organizationColor = [self colorWithHexString:@"#ababab" alpha:1];


- (UIColor *)colorWithHexString:(NSString *)str_HEX alpha:(CGFloat)alpha_range{
int red = 0;
int green = 0;
int blue = 0;
sscanf([str_HEX UTF8String], "#%02X%02X%02X", &red, &green, &blue);
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4265161%2fhow-to-convert-hexadecimal-to-rgb%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    82














    I've just expanded my UIColor Category for you.

    use it like UIColor *green = [UIColor colorWithHexString:@"#00FF00"];



    //
    // UIColor_Categories.h
    //
    // Created by Matthias Bauch on 24.11.10.
    // Copyright 2010 Matthias Bauch. All rights reserved.
    //

    #import <Foundation/Foundation.h>


    @interface UIColor(MBCategory)

    + (UIColor *)colorWithHex:(UInt32)col;
    + (UIColor *)colorWithHexString:(NSString *)str;

    @end

    //
    // UIColor_Categories.m
    //
    // Created by Matthias Bauch on 24.11.10.
    // Copyright 2010 Matthias Bauch. All rights reserved.
    //

    #import "UIColor_Categories.h"

    @implementation UIColor(MBCategory)

    // takes @"#123456"
    + (UIColor *)colorWithHexString:(NSString *)str {
    const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];
    long x = strtol(cStr+1, NULL, 16);
    return [UIColor colorWithHex:x];
    }

    // takes 0x123456
    + (UIColor *)colorWithHex:(UInt32)col {
    unsigned char r, g, b;
    b = col & 0xFF;
    g = (col >> 8) & 0xFF;
    r = (col >> 16) & 0xFF;
    return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1];
    }

    @end





    share|improve this answer
























    • I know this is old now but I believe that you have to #import <UIKit/UIColor.h> to actual be able to do UIColor(MBCategory) otherwise you get an error saying unable to find @interface UIColor. This could just have been an update since this was written, but it doesn't work without the import.

      – Popeye
      Jul 11 '12 at 14:27













    • It should work in a standard iOS project created by Xcode. The whole UIKit is imported in the precompiled header (ProjectName-Prefix.pch).

      – Matthias Bauch
      Jul 11 '12 at 16:22


















    82














    I've just expanded my UIColor Category for you.

    use it like UIColor *green = [UIColor colorWithHexString:@"#00FF00"];



    //
    // UIColor_Categories.h
    //
    // Created by Matthias Bauch on 24.11.10.
    // Copyright 2010 Matthias Bauch. All rights reserved.
    //

    #import <Foundation/Foundation.h>


    @interface UIColor(MBCategory)

    + (UIColor *)colorWithHex:(UInt32)col;
    + (UIColor *)colorWithHexString:(NSString *)str;

    @end

    //
    // UIColor_Categories.m
    //
    // Created by Matthias Bauch on 24.11.10.
    // Copyright 2010 Matthias Bauch. All rights reserved.
    //

    #import "UIColor_Categories.h"

    @implementation UIColor(MBCategory)

    // takes @"#123456"
    + (UIColor *)colorWithHexString:(NSString *)str {
    const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];
    long x = strtol(cStr+1, NULL, 16);
    return [UIColor colorWithHex:x];
    }

    // takes 0x123456
    + (UIColor *)colorWithHex:(UInt32)col {
    unsigned char r, g, b;
    b = col & 0xFF;
    g = (col >> 8) & 0xFF;
    r = (col >> 16) & 0xFF;
    return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1];
    }

    @end





    share|improve this answer
























    • I know this is old now but I believe that you have to #import <UIKit/UIColor.h> to actual be able to do UIColor(MBCategory) otherwise you get an error saying unable to find @interface UIColor. This could just have been an update since this was written, but it doesn't work without the import.

      – Popeye
      Jul 11 '12 at 14:27













    • It should work in a standard iOS project created by Xcode. The whole UIKit is imported in the precompiled header (ProjectName-Prefix.pch).

      – Matthias Bauch
      Jul 11 '12 at 16:22
















    82












    82








    82







    I've just expanded my UIColor Category for you.

    use it like UIColor *green = [UIColor colorWithHexString:@"#00FF00"];



    //
    // UIColor_Categories.h
    //
    // Created by Matthias Bauch on 24.11.10.
    // Copyright 2010 Matthias Bauch. All rights reserved.
    //

    #import <Foundation/Foundation.h>


    @interface UIColor(MBCategory)

    + (UIColor *)colorWithHex:(UInt32)col;
    + (UIColor *)colorWithHexString:(NSString *)str;

    @end

    //
    // UIColor_Categories.m
    //
    // Created by Matthias Bauch on 24.11.10.
    // Copyright 2010 Matthias Bauch. All rights reserved.
    //

    #import "UIColor_Categories.h"

    @implementation UIColor(MBCategory)

    // takes @"#123456"
    + (UIColor *)colorWithHexString:(NSString *)str {
    const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];
    long x = strtol(cStr+1, NULL, 16);
    return [UIColor colorWithHex:x];
    }

    // takes 0x123456
    + (UIColor *)colorWithHex:(UInt32)col {
    unsigned char r, g, b;
    b = col & 0xFF;
    g = (col >> 8) & 0xFF;
    r = (col >> 16) & 0xFF;
    return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1];
    }

    @end





    share|improve this answer













    I've just expanded my UIColor Category for you.

    use it like UIColor *green = [UIColor colorWithHexString:@"#00FF00"];



    //
    // UIColor_Categories.h
    //
    // Created by Matthias Bauch on 24.11.10.
    // Copyright 2010 Matthias Bauch. All rights reserved.
    //

    #import <Foundation/Foundation.h>


    @interface UIColor(MBCategory)

    + (UIColor *)colorWithHex:(UInt32)col;
    + (UIColor *)colorWithHexString:(NSString *)str;

    @end

    //
    // UIColor_Categories.m
    //
    // Created by Matthias Bauch on 24.11.10.
    // Copyright 2010 Matthias Bauch. All rights reserved.
    //

    #import "UIColor_Categories.h"

    @implementation UIColor(MBCategory)

    // takes @"#123456"
    + (UIColor *)colorWithHexString:(NSString *)str {
    const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];
    long x = strtol(cStr+1, NULL, 16);
    return [UIColor colorWithHex:x];
    }

    // takes 0x123456
    + (UIColor *)colorWithHex:(UInt32)col {
    unsigned char r, g, b;
    b = col & 0xFF;
    g = (col >> 8) & 0xFF;
    r = (col >> 16) & 0xFF;
    return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1];
    }

    @end






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '10 at 11:48









    Matthias BauchMatthias Bauch

    83.4k15206242




    83.4k15206242













    • I know this is old now but I believe that you have to #import <UIKit/UIColor.h> to actual be able to do UIColor(MBCategory) otherwise you get an error saying unable to find @interface UIColor. This could just have been an update since this was written, but it doesn't work without the import.

      – Popeye
      Jul 11 '12 at 14:27













    • It should work in a standard iOS project created by Xcode. The whole UIKit is imported in the precompiled header (ProjectName-Prefix.pch).

      – Matthias Bauch
      Jul 11 '12 at 16:22





















    • I know this is old now but I believe that you have to #import <UIKit/UIColor.h> to actual be able to do UIColor(MBCategory) otherwise you get an error saying unable to find @interface UIColor. This could just have been an update since this was written, but it doesn't work without the import.

      – Popeye
      Jul 11 '12 at 14:27













    • It should work in a standard iOS project created by Xcode. The whole UIKit is imported in the precompiled header (ProjectName-Prefix.pch).

      – Matthias Bauch
      Jul 11 '12 at 16:22



















    I know this is old now but I believe that you have to #import <UIKit/UIColor.h> to actual be able to do UIColor(MBCategory) otherwise you get an error saying unable to find @interface UIColor. This could just have been an update since this was written, but it doesn't work without the import.

    – Popeye
    Jul 11 '12 at 14:27







    I know this is old now but I believe that you have to #import <UIKit/UIColor.h> to actual be able to do UIColor(MBCategory) otherwise you get an error saying unable to find @interface UIColor. This could just have been an update since this was written, but it doesn't work without the import.

    – Popeye
    Jul 11 '12 at 14:27















    It should work in a standard iOS project created by Xcode. The whole UIKit is imported in the precompiled header (ProjectName-Prefix.pch).

    – Matthias Bauch
    Jul 11 '12 at 16:22







    It should work in a standard iOS project created by Xcode. The whole UIKit is imported in the precompiled header (ProjectName-Prefix.pch).

    – Matthias Bauch
    Jul 11 '12 at 16:22















    25














    //In your header file  

    #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

    //usage
    UIColor *color = UIColorFromRGB(0x000000)
    //you can also use it inline
    [text.textField setTextColor:UIColorFromRGB(0xcccccc)];





    share|improve this answer



















    • 2





      I think this is something great :)

      – Munim Dibosh
      Jun 14 '13 at 8:47
















    25














    //In your header file  

    #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

    //usage
    UIColor *color = UIColorFromRGB(0x000000)
    //you can also use it inline
    [text.textField setTextColor:UIColorFromRGB(0xcccccc)];





    share|improve this answer



















    • 2





      I think this is something great :)

      – Munim Dibosh
      Jun 14 '13 at 8:47














    25












    25








    25







    //In your header file  

    #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

    //usage
    UIColor *color = UIColorFromRGB(0x000000)
    //you can also use it inline
    [text.textField setTextColor:UIColorFromRGB(0xcccccc)];





    share|improve this answer













    //In your header file  

    #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

    //usage
    UIColor *color = UIColorFromRGB(0x000000)
    //you can also use it inline
    [text.textField setTextColor:UIColorFromRGB(0xcccccc)];






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '10 at 19:26









    Andrew CarterAndrew Carter

    1,92011219




    1,92011219








    • 2





      I think this is something great :)

      – Munim Dibosh
      Jun 14 '13 at 8:47














    • 2





      I think this is something great :)

      – Munim Dibosh
      Jun 14 '13 at 8:47








    2




    2





    I think this is something great :)

    – Munim Dibosh
    Jun 14 '13 at 8:47





    I think this is something great :)

    – Munim Dibosh
    Jun 14 '13 at 8:47











    5














    Swift




    It is very useful




    extension UIColor {

    convenience init(hexString: String) {
    let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
    var int = UInt32()
    Scanner(string: hex).scanHexInt32(&int)
    let a, r, g, b: UInt32
    switch hex.count {
    case 3: // RGB (12-bit)
    (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
    case 6: // RGB (24-bit)
    (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
    case 8: // ARGB (32-bit)
    (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
    default:
    (a, r, g, b) = (1, 1, 1, 0)
    }
    self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
    }

    public func hexStringWithAlpha(_ includeAlpha: Bool) -> String {
    var r: CGFloat = 0
    var g: CGFloat = 0
    var b: CGFloat = 0
    var a: CGFloat = 0
    self.getRed(&r, green: &g, blue: &b, alpha: &a)

    if (includeAlpha) {
    return String(format: "%02X%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255), Int(a * 255))
    } else {
    return String(format: "%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
    }
    }

    open override var description: String {
    return self.hexStringWithAlpha(true)
    }

    open override var debugDescription: String {
    return self.hexStringWithAlpha(true)
    }
    }


    Obj-C



    UIColor *organizationColor = [self colorWithHexString:@"#ababab" alpha:1];


    - (UIColor *)colorWithHexString:(NSString *)str_HEX alpha:(CGFloat)alpha_range{
    int red = 0;
    int green = 0;
    int blue = 0;
    sscanf([str_HEX UTF8String], "#%02X%02X%02X", &red, &green, &blue);
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
    }





    share|improve this answer






























      5














      Swift




      It is very useful




      extension UIColor {

      convenience init(hexString: String) {
      let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
      var int = UInt32()
      Scanner(string: hex).scanHexInt32(&int)
      let a, r, g, b: UInt32
      switch hex.count {
      case 3: // RGB (12-bit)
      (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
      case 6: // RGB (24-bit)
      (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
      case 8: // ARGB (32-bit)
      (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
      default:
      (a, r, g, b) = (1, 1, 1, 0)
      }
      self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
      }

      public func hexStringWithAlpha(_ includeAlpha: Bool) -> String {
      var r: CGFloat = 0
      var g: CGFloat = 0
      var b: CGFloat = 0
      var a: CGFloat = 0
      self.getRed(&r, green: &g, blue: &b, alpha: &a)

      if (includeAlpha) {
      return String(format: "%02X%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255), Int(a * 255))
      } else {
      return String(format: "%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
      }
      }

      open override var description: String {
      return self.hexStringWithAlpha(true)
      }

      open override var debugDescription: String {
      return self.hexStringWithAlpha(true)
      }
      }


      Obj-C



      UIColor *organizationColor = [self colorWithHexString:@"#ababab" alpha:1];


      - (UIColor *)colorWithHexString:(NSString *)str_HEX alpha:(CGFloat)alpha_range{
      int red = 0;
      int green = 0;
      int blue = 0;
      sscanf([str_HEX UTF8String], "#%02X%02X%02X", &red, &green, &blue);
      return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
      }





      share|improve this answer




























        5












        5








        5







        Swift




        It is very useful




        extension UIColor {

        convenience init(hexString: String) {
        let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int = UInt32()
        Scanner(string: hex).scanHexInt32(&int)
        let a, r, g, b: UInt32
        switch hex.count {
        case 3: // RGB (12-bit)
        (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
        (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
        (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
        (a, r, g, b) = (1, 1, 1, 0)
        }
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
        }

        public func hexStringWithAlpha(_ includeAlpha: Bool) -> String {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        self.getRed(&r, green: &g, blue: &b, alpha: &a)

        if (includeAlpha) {
        return String(format: "%02X%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255), Int(a * 255))
        } else {
        return String(format: "%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
        }
        }

        open override var description: String {
        return self.hexStringWithAlpha(true)
        }

        open override var debugDescription: String {
        return self.hexStringWithAlpha(true)
        }
        }


        Obj-C



        UIColor *organizationColor = [self colorWithHexString:@"#ababab" alpha:1];


        - (UIColor *)colorWithHexString:(NSString *)str_HEX alpha:(CGFloat)alpha_range{
        int red = 0;
        int green = 0;
        int blue = 0;
        sscanf([str_HEX UTF8String], "#%02X%02X%02X", &red, &green, &blue);
        return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
        }





        share|improve this answer















        Swift




        It is very useful




        extension UIColor {

        convenience init(hexString: String) {
        let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int = UInt32()
        Scanner(string: hex).scanHexInt32(&int)
        let a, r, g, b: UInt32
        switch hex.count {
        case 3: // RGB (12-bit)
        (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
        (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
        (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
        (a, r, g, b) = (1, 1, 1, 0)
        }
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
        }

        public func hexStringWithAlpha(_ includeAlpha: Bool) -> String {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        self.getRed(&r, green: &g, blue: &b, alpha: &a)

        if (includeAlpha) {
        return String(format: "%02X%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255), Int(a * 255))
        } else {
        return String(format: "%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
        }
        }

        open override var description: String {
        return self.hexStringWithAlpha(true)
        }

        open override var debugDescription: String {
        return self.hexStringWithAlpha(true)
        }
        }


        Obj-C



        UIColor *organizationColor = [self colorWithHexString:@"#ababab" alpha:1];


        - (UIColor *)colorWithHexString:(NSString *)str_HEX alpha:(CGFloat)alpha_range{
        int red = 0;
        int green = 0;
        int blue = 0;
        sscanf([str_HEX UTF8String], "#%02X%02X%02X", &red, &green, &blue);
        return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 10:21

























        answered Sep 16 '15 at 10:41









        Jaleel NazirJaleel Nazir

        4,11032541




        4,11032541






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4265161%2fhow-to-convert-hexadecimal-to-rgb%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory