728x90
반응형

Gradient 를 파랑(0) 초록(0.5) 노랑(1) 으로 표현을 한 경우

% 에 따른 색상을 구할때 사용한다.

 

        #region GetColorByOffset(double)

        /// <summary>

        /// Gradient 색의 offset 값을 구합니다.

        /// </summary>

        /// <param name="offset"></param>

        /// <returns></returns>

        private Color GetColorByOffset(double offset)

        {

            GradientStopCollection collection = new GradientStopCollection(3);

            collection.Add(new GradientStop(Colors.Blue, 0));

            collection.Add(new GradientStop(Colors.Green, 0.5));

            collection.Add(new GradientStop(Colors.Yellow, 1));

 

            GradientStop[] stops = collection.OrderBy(x => x.Offset).ToArray();

            if (offset <= 0) return stops[0].Color;

            if (offset >= 1) return stops[stops.Length - 1].Color;

            GradientStop left = stops[0], right = null;

            foreach (GradientStop stop in stops)

            {

                if (stop.Offset >= offset)

                {

                    right = stop;

                    break;

                }

                left = stop;

            }

 

            //Debug.Assert(right != null);

            offset = Math.Round((offset - left.Offset) / (right.Offset - left.Offset), 2);

            byte a = (byte)((right.Color.A - left.Color.A) * offset + left.Color.A);

            byte r = (byte)((right.Color.R - left.Color.R) * offset + left.Color.R);

            byte g = (byte)((right.Color.G - left.Color.G) * offset + left.Color.G);

            byte b = (byte)((right.Color.B - left.Color.B) * offset + left.Color.B);

            return Color.FromArgb(a, r, g, b);

        }

        #endregion

 

728x90
반응형

+ Recent posts