gradient color

本文介绍了在iOS开发中如何手动设置CALayer的布局,并通过调整UIView的子视图实现自定义边框和渐变效果。同时,提供了获取颜色深浅变化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.cnblogs.com/YouXianMing/p/3793913.html

 

 


 

layer 不能自动autolay, 只能在viewDidLayout里面设置宽度

 

- (void) viewDidLayoutSubviews {
  [super viewDidLayoutSubviews]; //if you want superclass's behaviour... 
  // resize your layers based on the view's new frame
  self.editViewBorderLayer.frame = self.editView.bounds;
}

  

gradientLayer = [CAGradientLayer layer];
_gradientLayer.frame = maskedImageView.bounds;
_gradientLayer.colors = colors;

//set locations for the colors
NSArray * startingLocations = @[@0.0, @0.4,@1.0];
NSArray *endinglocations = @[@0.0,@0.8,@1.0];

// Update the model layer to the final point
_gradientLayer.locations = endinglocations;
_gradientLayer.startPoint = CGPointMake(0.0f, 0.3f);
_gradientLayer.endPoint = CGPointMake(1.0f, 0.5f);

//add the text image as a mask on the gradient layer
_gradientLayer.mask = maskedImageView.layer;

//add the gradient layer to the holder view
[_slideImageView.layer addSublayer:_gradientLayer];

  

location 代表的是分界线, 比如三个的话就为 @[@(0), @(5), @(1)]

 

 


 

https://github.com/yannickl/DynamicColor

 


208
down vote
accepted
- (UIColor *)lighterColorForColor:(UIColor *)c
{
    CGFloat r, g, b, a;
    if ([c getRed:&r green:&g blue:&b alpha:&a])
        return [UIColor colorWithRed:MIN(r + 0.2, 1.0)
                               green:MIN(g + 0.2, 1.0)
                                blue:MIN(b + 0.2, 1.0)
                               alpha:a];
    return nil;
}

- (UIColor *)darkerColorForColor:(UIColor *)c
{
    CGFloat r, g, b, a;
    if ([c getRed:&r green:&g blue:&b alpha:&a])
        return [UIColor colorWithRed:MAX(r - 0.2, 0.0)
                               green:MAX(g - 0.2, 0.0)
                                blue:MAX(b - 0.2, 0.0)
                               alpha:a];
    return nil;
}

 

http://stackoverflow.com/questions/11598043/get-slightly-lighter-and-darker-color-from-uicolor

@implementation UIColor (LightAndDark)

- (UIColor *)lighterColor
{
    CGFloat h, s, b, a;
    if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
        return [UIColor colorWithHue:h
                          saturation:s
                          brightness:MIN(b * 1.3, 1.0)
                               alpha:a];
    return nil;
}

- (UIColor *)darkerColor
{
    CGFloat h, s, b, a;
    if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
        return [UIColor colorWithHue:h
                          saturation:s
                          brightness:b * 0.75
                               alpha:a];
    return nil;
}
@end

  


CGFloat red, green, blue, alpha;

//Create a sample color

UIColor *redColor = [UIColor redColor];

//Call 

[redColor getRed: &red 
  green: &green
  blue: &blue 
  alpha: &alpha];
NSLog(@"red = %f. Green = %f. Blue = %f. Alpha = %f",
  red,
  green,
  blue,

转载于:https://www.cnblogs.com/studyNT/p/5251258.html

### Cesium 中 ColorGradient 的用法及相关功能 Cesium 提供了一种灵活的方式来定义颜色渐变,这通常通过 `Cesium.Color` 和 `Cesium.Property` 类来实现。虽然 Cesium 并未直接提供名为 `ColorGradient` 的类,但可以通过组合多个属性和方法创建类似的颜色梯度效果。 以下是具体实现方式: #### 使用 `Cesium.Material` 创建颜色渐变 Cesium 支持自定义材质 (Material),可以利用这些材质来模拟颜色渐变的效果。下面是一个简单的例子,展示如何基于高度变化应用颜色渐变[^2]。 ```javascript // 定义一个基于高度的颜色渐变函数 function createHeightBasedColor Gradient(minHeight, maxHeight) { return new Cesium.ColorGeometryInstanceAttribute(function(feature) { var height = feature.properties.height; // 获取当前对象的高度 var normalizedHeight = Cesium.Math.clamp((height - minHeight) / (maxHeight - minHeight), 0.0, 1.0); // 根据高度计算颜色插值 var color = Cesium.Color.lerp( Cesium.Color.BLUE, Cesium.Color.RED, normalizedHeight, new Cesium.Color() ); return Cesium.ColorGeometryInstanceAttribute.toValue(color); }); } var viewer = new Cesium.Viewer('cesiumContainer'); viewer.entities.add({ name : 'Height-based gradient', position : Cesium.Cartesian3.fromDegrees(-75.164209, 39.985508, 100), box : { dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0), material : createHeightBasedColorGradient(0, 500000) } }); ``` 此代码片段展示了如何根据物体的高度动态调整其颜色,从而形成一种视觉上的颜色渐变效果。 #### 利用 `Cesium.CustomShader` 实现更复杂的颜色过渡 如果需要更加复杂或实时更新的颜色渐变逻辑,则可以借助 `Cesium.CustomShader` 来编写 GLSL 片段着色器脚本。这种方式允许开发者完全控制渲染管线中的每一帧像素处理过程[^3]。 ```javascript const customShader = new Cesium.CustomShader({ fragmentShaderText: ` void fragmentMain(FragmentInput fsInput, out vec4 fragColor) { float t = clamp(fsInput.positionWC.y / 1e6, 0.0, 1.0); // Normalize y-coordinate to [0..1] vec3 col = mix(vec3(0., 0., 1.), vec3(1., 0., 0.), t); // Blue -> Red transition based on altitude. fragColor = vec4(col, 1.); }`, }); viewer.scene.primitives.add(new Cesium.Primitive({ geometryInstances : [ new Cesium.GeometryInstance({ geometry : new Cesium.BoxGeometry({ vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT, minimumCorner : new Cesium.Cartesian3(-1e6,-1e6,-1e6), maximumCorner : new Cesium.Cartesian3( 1e6, 1e6, 1e6) }) }), ], appearance : new Cesium.PolylineColorAppearance(), customShader : customShader })); ``` 以上示例说明了如何使用 GLSL 编写自定义着色程序来自由操控模型表面每一点的颜色表现形式。 --- #### 总结 尽管 Cesium 没有专门针对 “color gradient” 设计独立 API 接口,但是它提供了足够的灵活性让用户能够轻松构建所需的功能模块。无论是简单场景下的静态配色方案还是高级需求里的动态交互体验都可以很好地满足开发人员的要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值