UIColor+FIRIAMHexString.m 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS || TARGET_OS_TV
  18. #import "FirebaseInAppMessaging/Sources/Util/UIColor+FIRIAMHexString.h"
  19. @implementation UIColor (HexString)
  20. + (UIColor *)firiam_colorWithHexString:(nullable NSString *)hexString {
  21. if (hexString.length < 7) {
  22. return nil;
  23. }
  24. unsigned rgbValue = 0;
  25. NSScanner *scanner = [NSScanner scannerWithString:hexString];
  26. [scanner setScanLocation:1]; // bypass '#' character
  27. if (![scanner scanHexInt:&rgbValue]) {
  28. // no valid heximal value is detected
  29. return nil;
  30. }
  31. return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0
  32. green:((rgbValue & 0xFF00) >> 8) / 255.0
  33. blue:(rgbValue & 0xFF) / 255.0
  34. alpha:1.0];
  35. }
  36. @end
  37. #endif // TARGET_OS_IOS || TARGET_OS_TV