动态创建不同亮度的颜色 [英] Dynamically creating colors with different brightness

查看:157
本文介绍了动态创建不同亮度的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个颜色,我只知道在运行时。使用这种颜色我想创建两种新的颜色,一个非常明亮和一个没有明亮的颜色版本。

I have a color, which I only know at runtime. Using this color i want to create two new colors, one very bright and one none bright version of the color.

为了澄清,说我有红色;我想为浅红色颜色和深红色颜色创建十六进制值。

So to clarify, say i have the color Red; I want to create the hex-value for a "Light red" color, and a "Dark red" color.

我该如何做?我的代码是使用GWT用Java编写的。

How would i go about doing this? My code is written in Java using GWT.

推荐答案

将颜色转换为HSB / HSV(色相饱和度 - 亮度/ )空间,并调整亮度向上为较亮,向下为较暗。然后再转换回来。在Java中:

Convert the colours to the HSB/HSV (Hue-Saturation-Brightness/Value ) space and adjust the Brightness up for lighter and down for darker. Then convert back again. In Java:

    import java.awt.Color;

    float hsbVals[] = Color.RGBtoHSB( originalColour.getRed(),
                                       originalColour.getGreen(),
                                       originalColour.getBlue(), null );
    Color highlight = Color.getHSBColor( hsbVals[0], hsbVals[1], 0.5f * ( 1f + hsbVals[2] ));
    Color shadow = Color.getHSBColor( hsbVals[0], hsbVals[1], 0.5f * hsbVals[2] );

HSB空间是为这种操作设计的。

The HSB space is designed for this kind of operation.

基本点是你只需要改变亮度术语来得到你想要的增亮/变暗效果。

The essential point is that you only need to vary the Brightness term to get the lightening/darkening effect you want. You'll have to experiment with how much you lighten/darken.

上面的代码将亮度偏移到白色的中间位置,以突出显示和中途变黑为阴影。 (我使用此代码在按钮上创建突出显示的边框效果。)

The above code shifts the Brightness to half-way towards white for the highlight and half-way to black for the shadow. (I used this code to create a highlighted border effect on a button.)

请参阅: http://en.wikipedia.org/wiki/HSL_and_HSV http://www.acasystems.com/en/color-picker/faq-hsb-hsv-color.htm

编辑:根据评论, java.awt.Color 类不能用于GWT 。由于我们使用的 Color 类的唯一部分是HSV到RGB和RGB到HSV的转换,因为你使用的是GWT,你可以改为google的实现的那些算法: Google HSV RGB转换算法。例如:

According to the comments, the java.awt.Color class can't be used in GWT. Since the only part of theColor class we're using are the HSV to RGB and the RGB to HSV conversions, as you're using GWT you could instead google for an implementation of those algorithms: Google HSV RGB conversion algorithm. For example:

  • javascripter.net
  • cs.rit.edu/~ncs
  • rapidtables.com (RGB to HSV)
  • rapidtables.com (HSV to RGB)
  • StackOverflow: Algorithm to convert RGB to HSV and HSV to RGB?

这篇关于动态创建不同亮度的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆