Automatically change text color to assure readability

前端 未结 8 2033
生来不讨喜
生来不讨喜 2020-12-08 00:42

Users can set the background-color of a button through a textbox that accept RGB hexadecimal notation: ff00ff, ccaa22, etc. So I need to set the te

8条回答
  •  一生所求
    2020-12-08 01:48

    A bit late to the party, but IMO all text should either be light or dark. Colored text is for links.

    Here's a coffeescript function I've written to decide which to use:

    is_light = (hex_color) ->
      c = hex_color.replace(/^#/,'')
      sum = parseInt(c[0]+c[1], 16)
      sum += parseInt(c[2]+c[3], 16)
      sum += parseInt(c[4]+c[5], 16)  
      log "sum is #{sum}"
      sum > 382.6
    

提交回复
热议问题