A computer program product comprising a non-transitory computer readable storage medium having instructions encoded thereon that, when executed by a processor, cause the processor to:
- identify an opportunity to present content to a user of an online system;
- select a content item for presentation to the user, the content item comprising an image and text associated with the image;
- identify a segment of the image on which the text may be overlaid, the segment comprising a plurality of pixels forming a portion of the image;
- compute a brightness value describing a magnitude of brightness of at least a threshold number of pixels of the plurality of pixels comprising the segment;
- determine that the brightness value associated with the segment is greater than a lower threshold value and less than a high threshold value;
- determine that a readability of the text if overlaid on the segment is below a threshold readability;
- responsive to determining that the readability of the text is below the threshold readability:
- apply an opacity gradient to the segment, the opacity gradient modifying an opacity of a set of pixels of the plurality of pixels comprising the segment,
- select one or more characteristics of a font for the text based at least in part on the brightness value,
- generate a user interface comprising the content item, wherein the text associated with the image is overlaid on the segment in the font having the one or more characteristics, and
- present the user interface to the user.