<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Richard Natan</title>
    <description>The latest articles on DEV Community by Richard Natan (@richard_natan).</description>
    <link>https://dev.to/richard_natan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1242868%2Fc72d71ea-62a7-4770-95aa-2a57bdb59800.jpeg</url>
      <title>DEV Community: Richard Natan</title>
      <link>https://dev.to/richard_natan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/richard_natan"/>
    <language>en</language>
    <item>
      <title>Como extrair cores de uma imagem no Android Studio</title>
      <dc:creator>Richard Natan</dc:creator>
      <pubDate>Wed, 27 Dec 2023 22:15:40 +0000</pubDate>
      <link>https://dev.to/richard_natan/como-extrair-cores-de-uma-imagem-no-android-studio-9jk</link>
      <guid>https://dev.to/richard_natan/como-extrair-cores-de-uma-imagem-no-android-studio-9jk</guid>
      <description>&lt;p&gt;Olá pessoal, tudo bem? neste blog irei explicar como obter as cores de um bitmap no Android utilizando o Kotlin. Espero que tenha alguma serventia para você!&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependências
&lt;/h2&gt;

&lt;p&gt;Para buscar as cores iremos utilizar a biblioteca &lt;a href="https://developer.android.com/develop/ui/views/graphics/palette-colors?hl=pt-br" rel="noopener noreferrer"&gt;Palette&lt;/a&gt; do Jetpack. Para começar iremos declarar dentro do build.gradle(app):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Groovy
dependencies {
    ...
    implementation 'androidx.palette:palette:1.0.0'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ou:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Kotlin
dependencies {
    ...
    implementation("androidx.palette:palette:1.0.0")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Criar um Layout XML
&lt;/h2&gt;

&lt;p&gt;Estarei utilizando o seguinte layout XML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity"&amp;gt;

    &amp;lt;com.google.android.material.card.MaterialCardView
        android:layout_width="380dp"
        android:layout_height="270dp"&amp;gt;

        &amp;lt;ImageView
            android:id="@+id/photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/photo" /&amp;gt;

    &amp;lt;/com.google.android.material.card.MaterialCardView&amp;gt;

    &amp;lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mount Fuji"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" /&amp;gt;

    &amp;lt;LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_margin="10dp"&amp;gt;

        &amp;lt;TextView
            android:id="@+id/txt_light_vibranty"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Light Vibranty"
            android:textColor="@color/white" /&amp;gt;

        &amp;lt;TextView
            android:id="@+id/txt_vibranty"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Vibranty"
            android:textColor="@color/white" /&amp;gt;

        &amp;lt;TextView
            android:id="@+id/txt_dark_vibranty"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Dark Vibranty"
            android:textColor="@color/white" /&amp;gt;

        &amp;lt;TextView
            android:id="@+id/txt_light_muted"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Light Muted"
            android:textColor="@color/white" /&amp;gt;

        &amp;lt;TextView
            android:id="@+id/txt_muted"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Muted"
            android:textColor="@color/white" /&amp;gt;

        &amp;lt;TextView
            android:id="@+id/txt_dark_muted"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Dark Muted"
            android:textColor="@color/white" /&amp;gt;

    &amp;lt;/LinearLayout&amp;gt;

&amp;lt;/LinearLayout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Criar paleta de cores
&lt;/h2&gt;

&lt;p&gt;Para extrair as cores vamos criar uma função que recebe um ImageView ou um Bitmap se você possuir uma imagem que vem de uma API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; fun createPalette(image: ImageView) {
        // Este linha é para converter o drawable do imageView 
        // para bitmap
        val bitmap = image.drawable.toBitmap()

        val palette = Palette.from(bitmap).generate()

        view.showColors(palette)
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Extrair cores da paleta
&lt;/h2&gt;

&lt;p&gt;Para Extrair as cores criadas podemos criar uma função:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun showColors(response: Palette) {
        txtLightVibranty.setBackgroundColor(response.lightVibrantSwatch?.rgb ?: R.color.white)
        txtVibranty.setBackgroundColor(response.vibrantSwatch?.rgb ?: R.color.white)
        txtDarkVibranty.setBackgroundColor(response.darkVibrantSwatch?.rgb ?: R.color.white)
        txtLightMuted.setBackgroundColor(response.lightMutedSwatch?.rgb ?: R.color.white)
        txtMuted.setBackgroundColor(response.mutedSwatch?.rgb ?: R.color.white)
        txtDarkMuted.setBackgroundColor(response.darkMutedSwatch?.rgb ?: R.color.white)
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Utilizamos o &lt;code&gt;response.{nome da cor}?.rgb&lt;/code&gt; e definimos uma cor padrão caso seja nulo utilizando o operador elvis: &lt;code&gt;?: R.color.{cor padrão}&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finalização
&lt;/h2&gt;

&lt;p&gt;Resultado final:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwr27x3vaswdddahtgvuz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwr27x3vaswdddahtgvuz.png" alt="Resultado"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Este é o método mais simples e direto de se extrair cores de uma foto, espero que eu tenha lhe ajudado. Até a próxima! &lt;/p&gt;

</description>
      <category>android</category>
      <category>palette</category>
      <category>ui</category>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
