Objective-CでCSSでよく使うようなABCDEFFFのようなHex encodeされた形式の文字列からUIColorを生成する方法の紹介です。

objective-c>>
@interface UIColor (HexEncoding)

  • (UIColor*)colorFromString:(NSString*)string;
  • (NSString*)encodeToString;
    @end

@implementation UIColor (HexEncoding)

  • (UIColor*)
    colorFromString:(NSString*)string
    {
    NSScanner *scanner = [NSScanner scannerWithString:string];
    NSUInteger value;
    [scanner scanHexInt:&value];
    CGFloat red = ((value & 0xFF000000) >> 24) / 255.0f;
    CGFloat green = ((value & 0x00FF0000) >> 16) / 255.0f;
    CGFloat blue = ((value & 0x0000FF00) >> 8) / 255.0f;
    CGFloat alpha = ((value & 0x000000FF) >> 0) / 255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    }
  • (NSString*)encodeToString
    {
    const CGFloat *components = CGColorGetComponents(self.CGColor);
    return [NSString stringWithFormat:@"%02x%02x%02x%02x",
    (int)(components[0]*255 + 0.5f),
    (int)(components[1]*255 + 0.5f),
    (int)(components[2]*255 + 0.5f),
    (int)(components[3]*255 + 0.5f)];

}

@end
<<--

わかりやすいように、UIColorにメソッドカテゴリを追加しています。
encodeToStringのほうでは、0.5を足してroundをかけていますが、
状況に応じてfloorにしたりceilにしたりしてください。

また、この例ではComponentsのサイズが4である事を期待しています。
そうではない色を扱うときには問題が発生しますので、ご注意ください。

posted by genki genki on Fri 7 Nov 2008 at 12:10 with 0 comments
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
https://www.thegameroom.org/de/sportwetten/neu/ https://www.thegameroom.org/de/sportwetten/neu/: Развлечения для взрослых доступен через безопасные... 05/30 23:29
https://zwinkle.co/2026/05/24/emotional-availability-the-quality-that-determines-whether-love-can-actually-grow/ https://zwinkle.co/2026/05/24/emotional-availability-the-quality-that-determines-whether-love-can-actually-grow/: When some one searches for his required thing, the... 05/30 23:19
http://dailyplaza.co.kr/bbs/board.php?bo_table=1301&wr_id=297372 http://dailyplaza.co.kr/bbs/board.php?bo_table=1301&wr_id=297372: Experience Singapore’ѕ leading furniture store a... 05/30 22:47
https://meneercasino.com/slot-reviews/wolf-gold https://meneercasino.com/slot-reviews/wolf-gold: Смотрите видео для взрослых на безопасных и наде... 05/30 22:36
http://82.165.16.39/index.php?title=User:MadelineWysocki http://82.165.16.39/index.php?title=User:MadelineWysocki: пинап играть 05/30 22:28
http://series60.ru/go.php?url=https://www.518fans.com/ http://series60.ru/go.php?url=https://www.518fans.com/: Article writing is also a fun, if you be familiar ... 05/30 20:49
https://www.1337games.org/online-casinos/betaalmogelijkheden/ideal/ https://www.1337games.org/online-casinos/betaalmogelijkheden/ideal/: 优质xxx平台 提供高质量的成人娱乐内容。选择 有保障的平台 以获得安全且愉快的观看体验。 05/30 20:44
https://scapaflow.org https://scapaflow.org: Thank you for the good writeup. It in fact was a a... 05/30 19:28
Services from s21g
YOMU Web小説リーダー
小説投稿サイトの公式ページを開き、WebView表示と読み上げ向け表示を切り替えて、移動中や作業中にもWeb小説の続きを聴きやすくするiPhoneアプリです。
補助探
公開されている補助金・助成金情報を集約し、条件に合う制度を探しやすくするサービスです。
jotter.me
個人開発者のためのホスティング一体型ノートサービス
ハンドミラー
iPhone向けの手鏡アプリ
ツイプロ(twpro)
Twitterプロフィールの高速検索エンジン