Android color picking

Source: Internet
Author: User
Tags getcolor

/*
* Copyright (c) 2007 the android open source project
*
* Licensed under the Apache license, version 2.0 (the "License ");
* You may not use this file before t in compliance with the license.
* You may obtain a copy of the license
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the license is distributed on an "as is" basis,
* Without warranties or conditions of any kind, either express or implied.
* See the license for the specific language governing permissions and
* Limitations under the license.
*/

Package com. example. Android. APIs. graphics;

Import Android. OS. Bundle;
Import Android. App. Dialog;
Import Android. content. context;
Import Android. Graphics .*;
Import Android. View. motionevent;
Import Android. View. view;

Public class colorpickerdialog extends dialog {

Public interface oncolorchangedlistener {
Void colorchanged (INT color );
}

Private oncolorchangedlistener mlistener;
Private int minitialcolor;

Private Static class colorpickerview extends view {
Private paint mpaint;
Private paint mcenterpaint;
Private Final int [] mcolors;
Private oncolorchangedlistener mlistener;

Colorpickerview (context c, oncolorchangedlistener L, int color ){
Super (C );
Mlistener = L;
Mcolors = new int [] {
0xffff0000, 0xffff00ff, 0xff0000ff, 0xff00ffff, 0xff00ff00,
0xffffff00, 0xffff0000
};
Shader S = new sweepgradient (0, 0, mcolors, null );

Mpaint = new paint (paint. anti_alias_flag );
Mpaint. setshader (s );
Mpaint. setstyle (paint. style. Stroke );
Mpaint. setstrokewidth (32 );

Mcenterpaint = new paint (paint. anti_alias_flag );
Mcenterpaint. setcolor (color );
Mcenterpaint. setstrokewidth (5 );
}

Private Boolean mtrackingcenter;
Private Boolean mhighlightcenter;

@ Override
Protected void ondraw (canvas ){
Float r = center_x-mpaint. getstrokewidth () * 0.5f;

Canvas. Translate (center_x, center_x );

Canvas. drawoval (New rectf (-R,-R, R, R), mpaint );
Canvas. drawcircle (0, 0, center_radius, mcenterpaint );

If (mtrackingcenter ){
Int c = mcenterpaint. getcolor ();
Mcenterpaint. setstyle (paint. style. Stroke );

If (mhighlightcenter ){
Mcenterpaint. setalpha (0xff );
} Else {
Mcenterpaint. setalpha (0x80 );
}
Canvas. drawcircle (0, 0,
Center_radius + mcenterpaint. getstrokewidth (),
Mcenterpaint );

Mcenterpaint. setstyle (paint. style. Fill );
Mcenterpaint. setcolor (C );
}
}

@ Override
Protected void onmeasure (INT widthmeasurespec, int heightmeasurespec ){
Setmeasureddimension (center_x * 2, center_y * 2 );
}

Private Static final int center_x = 100;
Private Static final int center_y = 100;
Private Static final int center_radius = 32;

Private int floattobyte (float X ){
Int n = java. Lang. Math. Round (X );
Return N;
}
Private int pintobyte (int n ){
If (n <0 ){
N = 0;
} Else if (n> 255 ){
N = 255;
}
Return N;
}

Private int ave (int s, int D, float p ){
Return S + Java. Lang. Math. Round (p * (d-s ));
}

Private int interpcolor (INT colors [], float unit ){
If (Unit <= 0 ){
Return colors [0];
}
If (unit> = 1 ){
Return colors [colors. Length-1];
}

Float P = unit * (colors. Length-1 );
Int I = (INT) P;
P-= I;

// Now P is just the fractional part [0... 1) and I is the index
Int C0 = colors [I];
Int C1 = colors [I + 1];
Int A = ave (color. Alpha (C0), color. Alpha (C1), P );
Int r = ave (color. Red (C0), color. Red (C1), P );
Int G = ave (color. Green (C0), color. Green (C1), P );
Int B = ave (color. Blue (C0), color. Blue (C1), P );

Return color. argb (A, R, G, B );
}

Private int rotatecolor (INT color, float rad ){
Float deg = rad * 180/3 .1415927f;
Int r = color. Red (color );
Int G = color. Green (color );
Int B = color. Blue (color );

Colormatrix CM = new colormatrix ();
Colormatrix TMP = new colormatrix ();

Cm. setrgb2yuv ();
TMP. setrotate (0, deg );
Cm. postconcat (TMP );
TMP. setyuv2rgb ();
Cm. postconcat (TMP );

Final float [] A = cm. getarray ();

Int IR = floattobyte (A [0] * r + A [1] * g + A [2] * B );
Int ig = floattobyte (A [5] * r + A [6] * g + A [7] * B );
Int IB = floattobyte (A [10] * r + A [11] * g + A [12] * B );

Return color. argb (color. Alpha (color), pintobyte (IR ),
Pintobyte (IG), pintobyte (IB ));
}

Private Static final float Pi = 3.1415926f;

@ Override
Public Boolean ontouchevent (motionevent event ){
Float x = event. getx ()-center_x;
Float y = event. Gety ()-center_y;
Boolean incenter = java. Lang. Math. SQRT (x * x + y * Y) <= center_radius;

Switch (event. getaction ()){
Case motionevent. action_down:
Mtrackingcenter = incenter;
If (incenter ){
Mhighlightcenter = true;
Invalidate ();
Break;
}
Case motionevent. action_move:
If (mtrackingcenter ){
If (mhighlightcenter! = Incenter ){
Mhighlightcenter = incenter;
Invalidate ();
}
} Else {
Float angle = (float) Java. Lang. Math. atan2 (Y, X );
// Need to turn angle [-pi... Pi] into Unit [0... 1]
Float unit = angle/(2 * PI );
If (Unit <0 ){
Unit + = 1;
}
Mcenterpaint. setcolor (interpcolor (mcolors, unit ));
Invalidate ();
}
Break;
Case motionevent. action_up:
If (mtrackingcenter ){
If (incenter ){
Mlistener. colorchanged (mcenterpaint. getcolor ());
}
Mtrackingcenter = false; // so we draw w/o halo
Invalidate ();
}
Break;
}
Return true;
}
}

Public colorpickerdialog (context,
Oncolorchangedlistener listener,
Int initialcolor ){
Super (context );

Mlistener = listener;
Minitialcolor = initialcolor;
}

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Oncolorchangedlistener L = new oncolorchangedlistener (){
Public void colorchanged (INT color ){
Mlistener. colorchanged (color );
Dismiss ();
}
};

Setcontentview (New colorpickerview (getcontext (), L, minitialcolor ));
Settitle ("pick a color ");
}
}

Related Article

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.