Text color based on background image

后端 未结 2 1071
情歌与酒
情歌与酒 2020-12-04 18:36

My view has a background image with a text label overlay What\'s the best/good dynamic way to determine text color based on the background image so it can be readable (For n

2条回答
  •  春和景丽
    2020-12-04 19:07

    Find the average color with the link woz mentioned

    Then style your text

    CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
    [averageColor getRed:&red green:&green blue:&blue alpha:&alpha];
    
    int threshold = 105;
    int bgDelta = ((red * 0.299) + (green * 0.587) + (blue * 0.114));
    
    UIColor *textColor = (255 - bgDelta < threshold) ? [UIColor blackColor] : [UIColor whiteColor];
    

    something like this.

    You could also use the link above to get the UIColor from the image and use matt's category for UIColor to get light or dark.

提交回复
热议问题