Question

I'm an android/java newbie and am developing a simple app that will generate harmonious colours from a colour picked from a colour wheel. The problem is that I can't even get the right colour to appear in a box below the wheel (touching yellow on the colour wheel generates a purple colour :S ) I don't understand what's going on. I tried this code with several different images too, and the colours generated are still all off..

public class MainActivity extends Activity {

    ImageView cw;

    Bitmap cwBmp;

    ColourView colourArray;


    int redValue;

    int blueValue;

    int greenValue;

    int argb;

    int alpha; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        colourArray = new ColourView(this);
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
        rl.addView(colourArray);

        cw = (ImageView) findViewById(R.id.colourwheelpic);

         BitmapDrawable drawable = (BitmapDrawable) cw.getDrawable();
         cwBmp = drawable.getBitmap();

        cw.setOnTouchListener(new ImageView.OnTouchListener(){


            @Override
            public boolean onTouch(View arg0, MotionEvent event) {

                int touchX = (int)event.getX();
                  int touchY = (int)event.getY();
                  int pixel;
                  switch (event.getAction()) {
                         case MotionEvent.ACTION_DOWN:
                        touchX = (int)event.getX();
                        touchY = (int)event.getY();

                      case MotionEvent.ACTION_UP:
                      pixel = cwBmp.getPixel(touchX,touchY);
                      redValue = Color.red(pixel);


                    alpha = Color.alpha(pixel);
                    blueValue = Color.blue(pixel);
                        greenValue = Color.green(pixel);


                      argb = Color.argb(alpha, redValue,blueValue,greenValue);


                  colourArray.colors.clear();
                      colourArray.colors.add(argb);
                      colourArray.invalidate();
                      return true;
                  }

                  return false;

            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

The colourArray is a ColourView object

   public class ColourView extends View {

    Paint paint = new Paint();

    ArrayList<Integer> colors = new ArrayList<Integer>();



    public ColourView(Context context) {
        super(context);

        colors.add(Color.WHITE);    

    }

    public void onDraw(Canvas canvas){

        int i = 0;


        while ( i < 220 ){
                paint.setColor(colors.get(0));
                paint.setStrokeWidth(0);
                canvas.drawRect(25+ (44*i), 500,69+(44*i),550, paint);
                i = i + 1;
            }

        }

    private int getRandomInt(){
        int upper = colors.size();
        int r = (int) (Math.random() * (upper));
        return r;

    }


}

and here's my layout

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

    <ImageView
android:id="@+id/colourwheelpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/colourwheelbmp" />

    </RelativeLayout>
Was it helpful?

Solution

Now, without even reading your code, just by logic: If your YELLOW becomes PURPLE, then you're misplacing the GREEN and the BLUE RGB components.

In facts, this: #ffcccc00 is YELLOW and this #ffcc00cc is PURPLE.
So, you see that in #aarrggbb gg and bb are misplaced.

Now, by reading your code, I have the confirm:

argb = Color.argb(alpha, redValue,blueValue,greenValue);

should really be:

argb = Color.argb(alpha, redValue, greenValue, blueValue);

(a, r, g and b - not a, r, b and g)

;)

It happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top