`

color简单颜色渐变类

    博客分类:
  • Java
 
阅读更多
package com.my.baseclass;

public class Colour
{
//颜色#FFFFFF格式转化成RGB格式
public String GetColorRGB(String color)
{
    String str_rgb="";
    if(color.indexOf("#")==-1||color.length()!=7)
    {
     return str_rgb="";
    }
    else
    {
     String str_r=color.substring(1,3);
     String str_g=color.substring(3,5);
     String str_b=color.substring(5,7);
     int num_r=Integer.parseInt(str_r,16);
     int num_g=Integer.parseInt(str_g,16);
     int num_b=Integer.parseInt(str_b,16);
     str_rgb="RGB("+num_r+","+num_g+","+num_b+")";
    }
    return str_rgb;
}
//把RGB格式插分
public String GetRGBValue(String color)
{
    String str_color="";
    String num=color.substring(4).substring(0,color.substring(4).indexOf(")"));
    String num_r=num.substring(0,num.indexOf(","));
    String num_g1=num.substring(0,num.lastIndexOf(","));
    String num_g=num_g1.substring(num_g1.indexOf(",")).substring(1);
    String num_b=num.substring(num.lastIndexOf(",")).substring(1);
    str_color=num_r+","+num_g+","+num_b;
    return str_color;
}
//2个颜色的渐变
public String[] GetColor(String str1,String str2,String content)
{
    String rgb1=this.GetColorRGB(str1);
    String rgb2=this.GetColorRGB(str2);
    String var1=this.GetRGBValue(rgb1);
    String var2=this.GetRGBValue(rgb2);
    MyString myString=new MyString();
    String temp1[]=myString.StringSplit(var1,",");
    String temp2[]=myString.StringSplit(var2,",");
    int num=content.length();
    String NewRGB[] = new String[num];
    String color[]=new String[num];
    for(int k=0;k<num;k++)
    {
     for(int i=0;i<3;i++)
     {
      NewRGB[i]=String.valueOf(Integer.parseInt(temp1[i])+(Integer.parseInt(temp2[i])-Integer.parseInt(temp1[i]))*k/num);
      color[k]=NewRGB[0]+","+NewRGB[1]+","+NewRGB[2];
     }
     color[k]="RGB("+color[k]+")"+","+content.substring(k,k+1);
    }
    return color;
}
//主函数用于测试
public static void main(String[] args)
{
    Colour colour=new Colour();
    String[] test=colour.GetColor("#00FF33","#FFFF66","测试颜色");
}
}

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics