【问题标题】:Text color relative to image behind it相对于其背后图像的文本颜色
【发布时间】:2013-04-10 08:04:01
【问题描述】:

我有一种强烈的感觉,这是不可能的 - 但这就是我在这里的原因。

我有一个网站,它的主页上有一个全屏幻灯片,一个带有透明背景和深色文本/内容的标题。问题是在较暗的图像上您看不到内容,因为它也很暗。

显然我可以给 div 一个背景,但这不是设计所要求的。

有什么方法可以让文本相对于背景着色,可能是“反转”颜色效果或类似的东西?

提前致谢

【问题讨论】:

    标签: javascript jquery css colors


    【解决方案1】:
    1. 将信息存储在文件名中(最简单) - 例如 image1_light.png、imagex_dark.jpg 并查看 _light 或 _dark。或者

    2. How to pick good contrast RGB colors programmatically?

    3. canvas/html5 Using JavaScript or jQuery, how can I get the RGB color where ever the mouse is moving specially in <img> or <div> elements

    要看的demo是http://www.script-tutorials.com/demos/158/index.html,这里需要确定文字的位置,看看文字下面的主色是什么

    $(".navigation").position() 是 {上:30,左:381}
    $(".navigation").width()) 是 214
    $(".navigation").height()) 是 68

    我认为代码应该是这样的

    WORKING EXAMPLE

    var ctx,canvas;   
    $(function(){ // on page load, this should likely be on image transition
    
      // creating canvas object in memory. 
      // Since I do not append it anywhere it is not rendered
      canvas = $("<canvas/>")[0]; // a jQuery object on its own does not work
      ctx = canvas.getContext('2d');
    
      var image = new Image(); // create an image object in memory
      image.onload = function () {
          // resize
          canvas.width=this.width;
          canvas.height=this.height; 
          // render the image on the canvas
          ctx.drawImage(image, 0, 0); 
          var nav =  $(".navigation"); // get the navigation container
          // find a pixel about in the middle where the navigation would be
          var pos = {left: nav.position().left+(nav.width()/2), top: nav.position().top+(nav.height()/2) }
          var pixel = ctx.getImageData(pos.left,pos.top, 1, 1).data;
          $canvas=null; // no longer need this canvas 
          var invertedPixelColor = "rgba("+(255-pixel[0])+", "+(255-pixel[1])+", "+(255-pixel[2])+", "+1+")"; // invert it, ignoring the alpha channel
          nav.css("color",invertedPixelColor); // set the nav text to inverted color
          // here you could save the colour and reuse it 
          // if the user navigates to the same image
      }
      image.src = $(body).css('background-image'); // load the image, triggering the calc
    
    });
    

    【讨论】:

    • 如果我把canvas元素放在img元素上面,我还能找到这样的颜色吗?
    • 就像教程中一样,你必须在画布上渲染图像。
    • 我不渲染画布。假设图像是窗口的大小(否则我们将需要做其他事情)
    • 我认为这绝对是我正在寻找的最接近的解决方案,所以我会给你那个我们都在努力争取的绿色小勾。如果有随机不同颜色的像素,您是否认为从定义区域获得平均颜色会很复杂?我想我只是添加几个像素的颜色和/不。像素?
    • @mplungjan 很遗憾我从来没有经历过这个!我希望你能利用它!
    【解决方案2】:

    你可以通过蛮力来做到这一点。确定每个图像的文本颜色,然后在图像根据新图像的名称发生变化时使用 Javascript 设置颜色。当然,这意味着如果您更改任何图像,您也必须更改 Javascript。

    【讨论】:

    • 咳咳 - 我相信这是我的建议 #1 :) - 为什么需要更改 javascript?只需存储 image1_dark.png image23_light.png
    • 我假设需要的不仅仅是浅色或深色文本,其中每个图像可能需要单独的特定颜色文本,必须单独编码。在这种情况下,添加另一个图像需要添加代码来处理它。也许我把问题复杂化了。
    猜你喜欢
    • 2013-09-27
    • 1970-01-01
    • 2013-06-25
    • 2015-12-31
    • 1970-01-01
    • 2014-10-12
    • 2017-04-16
    • 2021-08-03
    • 2018-04-27
    相关资源
    最近更新 更多