Colorutil "Color tool class (color integer, RGB array, 16 binary conversion)"

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective

Mainly used for color integer, RGB array, 16 binary conversion (-12590395 <--> #3FE2C5 <--> [63,226,197])

No need for temporary

Code Analysis

The value of the int type value of color goes to 16 binary type values includes two scenarios:

Scenario One: Thinking: Calculate the value of the &16777215 and then get a 16 binary value from the string.

    /** Color int integer to color 16 binary color value "scheme one"     * colorint- -12590395     * return color 16 binary color value-#3FE2C5     */ public    static String int2hex (int  colorint) {        = "";         = String.Format ("#%06x", Integer.valueof (16777215 & colorint));         return hexcode;    }

Scenario Two: Ideas: Type INT----RGB array--and 16 binary type value

    /** Color int integer to color 16 binary color value "scheme two"     * colorint- -12590395     * return color 16 binary color value-#3FE2C5     */ public    static String int2hex2 (int  colorint) {        = "";         int [] RGB = Int2rgb (colorint);         = Rgb2hex (RGB);         return hexcode;    }
Use step one, Project organization chart

Precautions:

1. Need change package name and re-import r file path after importing class file

2, the values directory of files (Strings.xml, Dimens.xml, Colors.xml, etc.), if the project exists, then copy the contents inside, do not cover the entire

Second, the import procedure

Copy the Colorutil file to your project.

 PackageCom.why.project.colorutildemo.util;ImportAndroid.graphics.Color;/*** Created by haiyuking * Used Color tool class (color integer, RGB array, 16-binary conversion)*/ Public classColorutil {/**Color int integer to color 16 binary color value "scheme one" * colorint- -12590395 * return color 16 binary color value-#3FE2C5 **/     Public StaticString Int2hex (intcolorint) {String Hexcode= ""; Hexcode= String.Format ("#%06x", Integer.valueof (16777215 &colorint)); returnHexcode; }    /**Color int integer to color 16 binary color value "scheme two" * colorint- -12590395 * return color 16 binary color value-#3FE2C5 **/     Public StaticString Int2hex2 (intcolorint) {String Hexcode= ""; int[] RGB =Int2rgb (Colorint); Hexcode=Rgb2hex (RGB); returnHexcode; }    /**RGB array of color int integer to color * colorint- -12590395 * return color RGB array--[63,226,197] **/     Public Static int[] Int2rgb (intcolorint) {        int[] RGB =New int[]{0,0,0}; intRed =color.red (Colorint); intGreen =Color.green (Colorint); intBlue =Color.Blue (Colorint); rgb[0] =Red; rgb[1] =Green; rgb[2] =Blue; returnRGB; }    /**RGB Array to color 16 binary color value * RGB-RGB Array--[63,226,197] * return color 16 color value-#3FE2C5 **/     Public StaticString Rgb2hex (int[] RGB) {String Hexcode="#";  for(inti=0;i<rgb.length;i++){            intRgbitem =Rgb[i]; if(Rgbitem < 0) {Rgbitem= 0; }Else if(Rgbitem > 255) {Rgbitem= 255; } string[] Code= {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; intLcode = RGBITEM/16;//first acquisition, for example, 255/16 = =            intRCode = rgbitem% 16;//to obtain the remainder, for example, 255% = =Hexcode + = Code[lcode] + Code[rcode];//FF        }        returnHexcode; }    /**Color 16 binary color value to color int integer * colorhex-color 16 binary color value-#3FE2C5 * return colorint- -12590395 **/     Public Static inthex2int (String colorhex) {intColorint = 0; Colorint=Color.parsecolor (Colorhex); returnColorint; }    /**Color 16 binary color value to RGB array * colorhex-color 16 binary color value-#3FE2C5 * return color RGB array--[63,226,197] **/     Public Static int[] Hex2rgb (String colorhex) {intColorint =Hex2int (Colorhex); returnInt2rgb (Colorint); }    /**color RGB array to color int integer * rgb-color RGB array--[63,226,197] * return colorint- -12590395 **/     Public Static intRgb2int (int[] RGB) {        intColorint = 0; Colorint= Color.rgb (rgb[0],rgb[1],rgb[2]); returnColorint; }}
Iii. Methods of Use
 PackageCom.why.project.colorutildemo;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportCom.why.project.colorutildemo.util.ColorUtil; Public classMainactivityextendsappcompatactivity {Private Static FinalString TAG = "Mainactivity"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    InitData (); }    Private voidInitData () { String colorHex1 = Colorutil.int2hex (-12590395); //         #3FE2C5  String colorHex2 = colorutil.int2hex2 (-12590395); //         #3FE2C5         LOG.W (TAG, "colorhex1=" +colorhex1);        LOG.W (TAG,  "colorhex2=" +colorhex2);  int  [] Colorrgb = Colorutil.int2rgb (-12590395); // 63,226,197  LOG.W (TAG, "colorrgb=" +colorrgb[0]+ "," +colorrgb[1]+ "," +colorrgb[        2 int  colorInt1 = Colorutil.hex2int ("#3FE2C5"); //        -12590395  LOG.W (TAG, "colorint1=" + ) colorInt1); }}

Print the log as follows:

Confusing configuration

No

Resources

Temporary vacancies

Project Demo

Link: http://pan.baidu.com/s/1boWNxkb Password: KFLR

Colorutil "Color tool class (color integer, RGB array, 16 binary conversion)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.