From d096764aa541e145674b5f28ee74f36db5a96818 Mon Sep 17 00:00:00 2001 From: Anton Plebanovich Date: Thu, 3 Nov 2016 17:49:52 +0300 Subject: [PATCH] - Color components crash fix --- KVNProgress/Categories/UIColor+KVNContrast.m | 24 +++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/KVNProgress/Categories/UIColor+KVNContrast.m b/KVNProgress/Categories/UIColor+KVNContrast.m index 411bdb0..2e66f2c 100644 --- a/KVNProgress/Categories/UIColor+KVNContrast.m +++ b/KVNProgress/Categories/UIColor+KVNContrast.m @@ -12,14 +12,22 @@ @implementation UIColor (KVNContrast) - (UIStatusBarStyle)statusBarStyleConstrastStyle { - const CGFloat *componentColors = CGColorGetComponents(self.CGColor); - CGFloat darknessScore = (((componentColors[0] * 255) * 299) + ((componentColors[1] * 255) * 587) + ((componentColors[2] * 255) * 114)) / 1000; - - if (darknessScore >= 125) { - return UIStatusBarStyleDefault; - } - - return UIStatusBarStyleLightContent; + CGColorRef cgColor = self.CGColor; + size_t count = CGColorGetNumberOfComponents(cgColor); + const CGFloat *componentColors = CGColorGetComponents(cgColor); + + CGFloat darknessScore = 0; + if (count == 2) { + darknessScore = (((componentColors[0]*255) * 299) + ((componentColors[0]*255) * 587) + ((componentColors[0]*255) * 114)) / 1000; + } else if (count == 4) { + darknessScore = (((componentColors[0]*255) * 299) + ((componentColors[1]*255) * 587) + ((componentColors[2]*255) * 114)) / 1000; + } + + if (darknessScore >= 125) { + return UIStatusBarStyleDefault; + } + + return UIStatusBarStyleLightContent; } @end