Mélanger les couleurs
Ce petit programme montre comment il est possible de mélanger (ajouter ou soustraire) des couleurs avec les opérateurs "bit-à-bit" de Java.
MainActivity.java
package com.prog101.testcouleurs;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
/*
Rouge : 11111111 11111111 11111111 11111111 00000000 00000000 00000000 00000000
Pas rouge : 00000000 00000000 00000000 00000000 11111111 11111111 11111111 11111111
|--- opacité ---| |---- rouge ----| |---- vert -----| |---- bleu -----|
*/
public class MainActivity extends AppCompatActivity {
// couleurs
public static final int NOIR_OPAQUE = 0xFF000000; // pour débuter
public static final int ROUGE = 0x00FF0000;
public static final int VERT = 0x0000FF00;
public static final int BLEU = 0x000000FF;
// masques (bits inversés par rapport à la couleur originale)
public static final int PAS_ROUGE = 0xFF00FFFF;
public static final int PAS_VERT = 0xFFFF00FF;
public static final int PAS_BLEU = 0xFFFFFF00;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int couleur = NOIR_OPAQUE;
TextView tv1 = findViewById(R.id.tv1);
// on ajoute du rouge
couleur = couleur | ROUGE;
tv1.setBackgroundColor(couleur);
TextView tv2 = findViewById(R.id.tv2);
// on ajoute du vert pour faire du jaune
couleur = couleur | VERT;
tv2.setBackgroundColor(couleur);
TextView tv3 = findViewById(R.id.tv3);
// on enlève le vert pour revenir au rouge
couleur = couleur & PAS_VERT;
tv3.setBackgroundColor(couleur);
TextView tv4 = findViewById(R.id.tv4);
// on enlève le rouge pour revenir au noir
couleur = couleur & PAS_ROUGE;
tv4.setBackgroundColor(couleur);
TextView tv5 = findViewById(R.id.tv5);
// on ajoute du bleu
couleur = couleur | BLEU;
tv5.setBackgroundColor(couleur);
TextView tv6 = findViewById(R.id.tv6);
// on ajoute du vert pour faire du cyan
couleur = couleur | VERT;
tv6.setBackgroundColor(couleur);
TextView tv7 = findViewById(R.id.tv7);
// on ajoute du rouge pour faire du blan
couleur = couleur | ROUGE;
tv7.setBackgroundColor(couleur);
TextView tv8 = findViewById(R.id.tv8);
// on enlève le bleu et le rouge pour faire du vert
couleur = couleur & PAS_BLEU & PAS_ROUGE;
tv8.setBackgroundColor(couleur);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp" />
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp" />
<TextView
android:id="@+id/tv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp" />
<TextView
android:id="@+id/tv6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp" />
<TextView
android:id="@+id/tv7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp" />
<TextView
android:id="@+id/tv8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp" />
</LinearLayout>
