1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.compose.ui.graphics.painter 18 19 import androidx.compose.ui.geometry.Size 20 import androidx.compose.ui.graphics.Color 21 import androidx.compose.ui.graphics.ColorFilter 22 import androidx.compose.ui.graphics.drawscope.DrawScope 23 24 /** [Painter] implementation used to fill the provided bounds with the specified color */ 25 class ColorPainter(val color: Color) : Painter() { 26 private var alpha: Float = 1.0f 27 28 private var colorFilter: ColorFilter? = null 29 onDrawnull30 override fun DrawScope.onDraw() { 31 drawRect(color = color, alpha = alpha, colorFilter = colorFilter) 32 } 33 applyAlphanull34 override fun applyAlpha(alpha: Float): Boolean { 35 this.alpha = alpha 36 return true 37 } 38 applyColorFilternull39 override fun applyColorFilter(colorFilter: ColorFilter?): Boolean { 40 this.colorFilter = colorFilter 41 return true 42 } 43 equalsnull44 override fun equals(other: Any?): Boolean { 45 if (this === other) return true 46 if (other !is ColorPainter) return false 47 48 if (color != other.color) return false 49 50 return true 51 } 52 hashCodenull53 override fun hashCode(): Int { 54 return color.hashCode() 55 } 56 toStringnull57 override fun toString(): String { 58 return "ColorPainter(color=$color)" 59 } 60 61 /** Drawing a color does not have an intrinsic size, return [Size.Unspecified] here */ 62 override val intrinsicSize: Size = Size.Unspecified 63 } 64